Skip to content

Instantly share code, notes, and snippets.

*, *:before, *:after {
-moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box;
}
@galen
galen / Timezones.php
Created March 15, 2013 13:49
Setting timezones with DateTime
$utc_time = '';
$date_obj = new DateTime($utc_time, new DateTimeZone("UTC"));
$date_obj->setTimeZone("America/Los_Angeles");
$display_date = $date_obj->format("Y-m-d H:i:s");
@galen
galen / PHP Enum.php
Created March 14, 2013 01:41
Enums in PHP
class DaysOfWeek
{
const Sunday = 0;
const Monday = 1;
const Tuesday = 2;
const Wednesday = 3;
const Thursday = 4;
const Friday = 5;
const Saturday = 6;
}
@galen
galen / Pagination.php
Created March 14, 2013 01:39
Given the current page, total pages, and viewport this function returns an object with all the info needed to create pagination.
function get_pagination( $current_page, $total_pages, $pagination_viewport ) {
$start_page = max( $current_page - $pagination_viewport, 1 );
$end_page = min( $start_page + ( $pagination_viewport * 2 ), $total_pages );
$start_page = max( $end_page - ( $pagination_viewport * 2 ), 1 );
$pages = range( $start_page, $end_page );
$first_page_link = $last_page_link = false;
if ( !in_array( 1, $pages ) ) {
$first_page_link = true;
}
@galen
galen / Get file extension.php
Last active December 14, 2015 22:28
Easy way to get a file's extension
pathinfo( $file, PATHINFO_EXTENSION );
@galen
galen / Dynamic method addition.php
Last active December 14, 2015 22:28
Dynamically add methods to objects with Closure:::bind
Trait Addable {
private $methods = array();
public function addMethod( $name, $callable ) {
$this->methods[$name] = Closure::bind( $callable, $this, get_class() );
}
public function __call( $method, array $args ) {
call_user_func_array( $this->methods[$method], $args );
class Combinations implements Iterator
{
protected $c = null;
protected $s = null;
protected $n = 0;
protected $k = 0;
protected $pos = 0;
function __construct($s, $k) {
if(is_array($s)) {
@galen
galen / Justify Text.php
Last active November 13, 2020 12:45
Justify text with php
------------------------------------------------
<?php
ini_set( 'display_errors', 'On' );
error_reporting( E_ALL );
$strings = array(
'hello world there ok then' => 'hello world there ok then',
'hello' => ' hello ',
'ok then' => 'ok then',