Tuesday, December 22, 2009

Check if a date is in the past

$diff = (number_format((time() - strtotime($date_to_check)) / (60 * 60 * 24), 2));

if ($diff > 0)
{
echo "$date is $diff days ago from today";
}

Monday, December 21, 2009

Ajax with JSON returned from response (Prototype)

Javascript code:
----------------
edit_info = function(id) {
url = 'www.test.com/get_info?id=' + id;
new Ajax.Request(url, {
method: 'post',
onComplete: function(req) {
var data = eval("("+req.responseText+")");
$('email').value = data['email'];
$('city').value = data['city'];
}
});
}

PHP code:
---------
public function get_info() {
$info = db.query('select * from info where id='.$_REQUEST['id']);
echo json_encode($info);
exit;
}

Thursday, December 17, 2009

CONCAT value in GROUP BY in mysql

You can actually concat-ing values returned by group by query by doing this:

SELECT GROUP_CONCAT(DISTINCT name ORDER BY name SEPARATOR ',')
FROM animals
WHERE type IN ('bird',''reptile)
GROUP BY type

Tuesday, December 15, 2009

First date and last date of month PHP

// for current month
$start_date = date("Y-m-d", strtotime(date('Y-m').'-01 00:00:00'));
$end_date = date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date('Y-m').'-01 00:00:00'))));


// for specific month
$start_date = date("Y-m-d", strtotime(date('Y').'-'.$month.'-01 00:00:00'));
$end_date = date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date('Y').'-'.$month.'-01 00:00:00'))));

Google Chart / Graph PHP Class Helper (by me)

//Here is a little helper:
//========================
class GoogleGraphHelper {
public $api_url = "";
public $request = array();
public $maxValue = 0;
public $colorSet = array('FF0000', '00FF00', '0000FF', '000000', 'FFFF00', '00FFFF');
public $markerSet = array('o','s','x','c','d','c','x','s','o');
private $datapointLimit = 100;
private $dapointSetLimit = 4;

public function __construct() {
$this->api_url = "http://chart.apis.google.com/chart?";
$this->request = array(
'chart_type' => "cht=lc",
'chart_size' => "chs=600x500",
'chart_data' => "",
'chart_colors' => "",
'chart_legends' => "",
'chart_labels' => "",
'chart_markers' => "",
);
}

public function get_graph($data, $xlabels = array(), $ylabels = array()) {
self::set_data($data);
self::set_colors($data);
self::set_legends($data);
self::set_labels($xlabels, $ylabels);
self::set_markers($data);
return $this->api_url.implode("&", $this->request);
}

public function encode($data, $maxValue) {
$simpleEncoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

$chartData = "";
$len = sizeof($data);
for ($i = 0; $i < $len; $i++) {
$currentValue = $data[$i];
if (is_numeric($currentValue) && $currentValue >= 0) {
$chartData .= substr($simpleEncoding, round((strlen($simpleEncoding)-1) * $currentValue / $maxValue), 1);
}
else {
$chartData .= '_';
}
}
return $chartData;
}

public function set_data($data = array()) {
self::find_max($data);
$encoded_data = array();
foreach ($data as $key => $info) {
$encoded_data[] = self::encode($info['values'], $this->maxValue);
}

$this->request['chart_data'] = "chd=s:".implode(",", $encoded_data);
}

public function set_size($width, $height) {
$this->request['chart_size'] = "chs={$width}x{$height}";
}

public function set_type($type) {
$this->request['chart_type'] = "cht={$type}";
}

public function set_colors(&$data) {
$this->request['chart_colors'] = "chco=";
$colors = array();
foreach($data as &$d) {
if (!isset($d['color']))
$d['color'] = array_pop($this->colorSet);
$colors[] = $d['color'];
}
$this->request['chart_colors'] .= implode(",", $colors);
}

public function set_legends($data) {
$labels = array_keys($data);
$this->request['chart_legends'] = "chdl=". implode("|",$labels);
}

public function set_labels($xlabels, $ylabels) {
$this->request['chart_labels'] = "chxt=y,x";
if (!empty($xlabels))
$this->request['chart_labels'] .= "&chxl=1:|".implode("|",$xlabels);

if (empty($ylabels))
$this->request['chart_labels'] .= "&chxr=0,0,{$this->maxValue}";
else
$this->request['chart_labels'] .= "&chxr=0,".implode(",", $ylablels);
}

public function set_markers(&$data) {
$this->request['chart_markers'] = 'chm=';
$markers = array();
$i = 0;
foreach($data as &$d) {
if (!isset($d['marker']))
$d['marker'] = array_pop($this->markerSet);
$markers[] = $d['marker'].",".$d['color'].",".$i.","."-1,10";
$i++;
}
$this->request['chart_markers'] .= implode("|", $markers);
}

public function find_max($data) {
foreach ($data as $d) {
$max = round(max($d['values']) * 1.10);
$this->maxValue = ($this->maxValue < $max)? $max: $this->maxValue;
}
}
}




//How to use:
//=============================
$data = array(
'uno' => array(
'values' => array(40,60,60,45,47,75,70,72),
),
'dos' => array(
'values' => array(50,70,70,55,57,85,80,82),
),
);

$gg = new GoogleGraphHelper();
$gg->set_size(300,200);
$url = $gg->get_graph($data, array("jan", "feb", "march"));
echo $url;

// it will return
// ==============================
http://chart.apis.google.com/chart?cht=lc&chs=300x200&chd=s:anndfxtv,gttkl301&chco=00FFFF,FFFF00&chdl=uno|dos&chxt=y,x&chxl=1:|jan|feb|march&chxr=0,0,94&chm=o,00FFFF,0,-1,10|s,FFFF00,1,-1,10

Wednesday, December 9, 2009

Date iteration PHP

$today = date('Y-m-d');

$date_start = $today
$date_end = date('Y-m-d', strtotime("{$date_start} + 30 days"));

while ($date_start != $date_end) {
echo "\n$date_start";
date_start = date('Y-m-d', strtotime("{date_start} + 1 day"));
}


===================
OUTPUT:
2009-12-01
2009-12-02
2009-12-03
.
.
.
and so on...