For example I have 2 dates (year and month) like :
2013-03
2013-01 <-- Jan. current month.
2012-10
So my code is :
// Get current month and year
$m = date('m');
$y = date('Y');
// So I do something to get highest date so result is..
$highest = "2013-03";
// Current month and year
$current = $y."-".$m;
// So I do something to get lowest date so result is..
$lowest = "2012-10";
// My idea is get lower month from variables
function do_lower($date){
$date = explode("-",$date);
$y = $date[0];
$m = $date[1];
if($m != "01") {
$m = $m-1;
$m = sprintf('%02d',$m);
return $y."-".$m;
} else {
return ($y-1)."-12";
}
}
// This is my simple function to get result between high and low
function different($h,$l) {
global $data;
$h = do_lower($h);
if($h!=$l){
$data[] = $h;
different($h,$l);
}
}
Then .. to do all functions like.. (JSON)
$data[] = $highest;
different($highest,$current);
$data[] = $current;
different($current,$lowest);
$data[] = $lowest;
So my result is correct
["2013-03","2013-02","2013-01","2012-12","2012-11","2012-10"]
Anyone can review my code ?
Thanks so much