Skip to content

Instantly share code, notes, and snippets.

@alexkolson
Created November 5, 2010 06:04
Show Gist options
  • Save alexkolson/663712 to your computer and use it in GitHub Desktop.
Save alexkolson/663712 to your computer and use it in GitHub Desktop.
coefficient of awesomness
<?php
class Printer {
private $arr;
private $callback;
public function printer($arr,$wrap) {
$this->arr = $arr;
$this->callback = $wrap;
}
public function json() {
$printString;
$arrLength = count($this->arr);
$counter = 1;
if ($this->callback) {
$printString.=$this->callback.'(';
}
$printString.='{';
foreach($this->arr as $key => $value) {
if ($counter < $arrLength) {
$printString.='"'.$key.'"'.':'.'"'.$value.'"'.',';
} else {
$printString.='"'.$key.'"'.':'.'"'.$value.'"';
}
$counter++;
}
$printString.='}';
if ($this->callback) {
$printString.=')';
}
echo $printString;
}
}
$callback = false;
function init() {
if (isset($_GET["timezone"])) {
run($_GET["timezone"]);
} else {
run("local");
}
}
function run($tz) {
if ($tz != "local") {
date_default_timezone_set($tz);
}
$date = date("M").date(" j g:i A");
$arr = array("date" => $date, "error" => "false");
if(isset($_GET["callback"])) {
global $callback;
$callback = $_GET["callback"];
$printer = new Printer($arr, $callback);
$printer->json();
} else {
$printer = new Printer($arr, $callback);
$printer->json();
}
}
try {
init();
} catch (Exception $e) {
$errorOutput = new Printer(array("error" => "true", "errors" => $e->getMessage()), $callback);
$errorOutput->json();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment