Revision: 27401
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at June 6, 2010 04:22 by metoikos
Initial Code
<?php
function get_rangeof_dates($time_frame= 'all_time')
{
$from_date= '';
$to_date= '';
switch($time_frame)
{
case 'all_time' :
$from_date= '1170-01-01 00:00:00';
$to_date= '2250-12-31 23:59:59';
break;
case 'today' :
$from_date= date('Y-m-d 00:00:00');
$to_date= date('Y-m-d 23:59:59');
break;
case 'yesterday' :
$from_date= date('Y-m-d 00:00:00', time() - 24 * 60 * 60);
$to_date= date('Y-m-d 23:59:59', time() - 24 * 60 * 60);
break;
case 'this_week' :
$from_date= date('Y-m-d 00:00:00', time() -(date('w', time()) * 60 * 60 * 24));
$to_date= date('Y-m-d 23:59:59', time() +((6 - date('w', time())) * 60 * 60 * 24));
break;
case 'last_week' :
$from_date= date('Y-m-d 00:00:00', time() -(date('w', time()) * 60 * 60 * 24) - 7 * 24 * 60 * 60);
$to_date= date('Y-m-d 23:59:59', time() +((6 - date('w', time())) * 60 * 60 * 24) - 7 * 24 * 60 * 60);
break;
case 'last_7_days' :
$additonal_query= 'created_at > ' . date('Y-m-d', time() - 7 * 24 * 60 * 60);
$from_date= date('Y-m-d 00:00:00', time() - 7 * 24 * 60 * 60);
$to_date= date('Y-m-d 23:59:59');
break;
case 'this_month' :
$from_date= date('Y-m-d 00:00:00', mktime(0, 0, 0, date('m'), 1, date('Y')));
$to_date= date('Y-m-d 23:59:59');
break;
case 'last_month' :
$from_date= date('Y-m-01 00:00:00', strtotime('-1 month', strtotime(date('Y-m-d'))));
$to_date= date('Y-m-d 23:59:59', strtotime('-1 day', strtotime(date('Y-m-01'))));
break;
case 'last_30_days' :
$from_date= date('Y-m-d 00:00:00', time() - 30 * 24 * 60 * 60);
$to_date= date('Y-m-d 23:59:59');
break;
case 'this_quarter' :
$month= date('m');
$year= date('Y');
if($month == 1 || $month == 2 || $month == 3)
{
$from_date= $year . '-01-01';
$to_date= $year . '-03-31';
}
elseif($month == 4 || $month == 5 || $month == 6)
{
$from_date= $year . '-04-01';
$to_date= $year . '-06-30';
}
elseif($month == 7 || $month == 8 || $month == 9)
{
$from_date= $year . '-07-01';
$to_date= $year . '-09-30';
}
else
{
$from_date= $year . '-10-01';
$to_date= $year . '-12-31';
}
break;
case 'last_quarter' :
$month= date('m');
$year= date('Y');
if($month == 1 || $month == 2 || $month == 3)
{
$from_date=($year -1) . '-10-01';
$to_date=($year -1) . '-12-31';
}
elseif($month == 4 || $month == 5 || $month == 6)
{
$from_date= $year . '-01-01';
$to_date= $year . '-03-31';
}
elseif($month == 7 || $month == 8 || $month == 9)
{
$from_date= $year . '-04-01';
$to_date= $year . '-06-30';
}
else
{
$from_date= $year . '-07-01';
$to_date= $year . '-09-30';
}
break;
case 'this_year' :
$from_date= date('Y-01-01 00:00:00');
$to_date= date('Y-12-31 23:59:59');
break;
case 'last_year' :
$from_date= date('Y-01-01 00:00:00', mktime(0, 0, 0, 01, 01, date('Y') - 1));
$to_date= date('Y-12-31 23:59:59', mktime(0, 0, 0, 12, 31, date('Y') - 1));
break;
}
return array($from_date, $to_date);
}
?>
Initial URL
http://www.sajithmr.me/algorithm-for-range-of-dates/
Initial Description
Initial Title
php date range function
Initial Tags
php, date
Initial Language
PHP