Skip to content

Instantly share code, notes, and snippets.

View davidreuss's full-sized avatar

David Reuss davidreuss

View GitHub Profile
var DateHelper = {
// Takes the format of "Jan 15, 2007 15:45:00 GMT" and converts it to a relative time
// Ruby strftime: %b %d, %Y %H:%M:%S GMT
time_ago_in_words_with_parsing: function(from) {
var date = new Date;
date.setTime(Date.parse(from));
return this.time_ago_in_words(date);
},
time_ago_in_words: function(from) {
@davidreuss
davidreuss / gist:63896
Created February 13, 2009 13:35
A bit hackish method, of getting named arguments in PHP
<?php
class NamedArguments {
static function init($args) {
$assoc = reset($args);
if (is_array($assoc)) {
$diff = array_diff(array_keys($assoc), array_keys($args));
if (empty($diff)) return $assoc;
trigger_error('Bad parameters: '.join(',',$diff), E_USER_ERROR);
}
<?php
class MyController extends Zend_Controller_Action {
public $contexts = array(
'request' => array('json', 'html')
);
public function requestAction() {
$this->view->fields = Foo::getFields();
// Here i want to build the form - but ONLY if context is 'html'
wget -m -k -K -E <URL> # archive entire site
#!/bin/sh
if [ $# -eq 2 ]; then
email="$1"
password="$2"
curl -L -d "email=$email&password=$password" -c ontv.cookie 'http://ontv.dk/scripts/users.php?action=login'
xmltvurl=$(curl -b ontv.cookie 'http://ontv.dk/xmltv' | perl -ne '/(http:\/\/ontv.dk\/xmltv\/\w+)/ and print $1')
curl -o xmltv.xml "$xmltvurl"
rm ontv.cookie
exit 0
@davidreuss
davidreuss / bash template with option parsing
Created April 15, 2009 11:53
Template for shell-script with option-parsing
#!/bin/bash
usage() {
echo >&2 "Usage: $0 [ -d directory | --dir directory ] [ -u user | --user user ] | --help"
}
invalid() {
echo >&2 "Invalid option $1. Try $0 --help to see available options."
}
@davidreuss
davidreuss / gist:102383
Created April 27, 2009 07:54
url shortener built on custom base system
// Thanks: http://snook.ca/archives/php/url-shortener
$codeset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$base = strlen($codeset);
$n = 300;
$converted = "";
while ($n > 0) {
$converted = substr($codeset, ($n % $base), 1) . $converted;
$n = floor($n/$base);
<?php
$connection = new PDO($dsn);
// 1.
$persons = new Table($connection, 'persons');
$joe = $table->createRow(array('name' => 'Joe'));
$persons->save($joe);
// 2.
$person = array(
git config gitex.exportbranch PHP5
<?php
class Helper_ActionCache extends Zend_Controller_Action_Helper_Abstract {
public function preDispatch() {
$this->fire('pre');
}
public function postDispatch() {
$this->fire('post');
}