Algorithm for range of dates
12 May 2010![]()
Here is a php function which returns from and to date as an array for a range:
<?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);
}
?>
Tags: algorithm, date, download, from date, last 7 days, last month, last quarter, last week, php, php code, range, source code, this month, this quarter, this week, this year, to date, today, yesterday


I am writing this topic in sajithmr.com, because i am getting a lot of queries regarding this project from the place I submitted , 


