14Nov/082
All Sorts of UNIX Timestamp Goodness
I'm writing some tracking software and came upon the need to be able to get the two timestamps (UNIX timestamp: the number of seconds elapsed since midnight Coordinated Universal Time (UTC) of January 1, 1970, not counting leap seconds) of a given period of time. This period of time would be represented in words via a GET parameter in my application. To you, I present my switch statement to produce those two timestamps.
The hardest of all was getting "this week last year". Please note that all weeks begin on Monday.
You can check the resulting timestamps using this cool online timestamp converter.
switch($period)
{
case "yesterday":
$timestamp_start = mktime(0, 0, 0, date("n"), date("d")-1, date("Y"));
$timestamp_stop = mktime(23, 59, 59, date("n"), date("d")-1, date("Y"));
break;
case "this_week":
$timestamp_start = mktime(0, 0, 0, date("n"), date("j"), date("Y")) - ((date("N")-1)*3600*24);
$timestamp_stop = time();
break;
case "this_month":
$timestamp_start = mktime(0,0,0, date("n"), 1, date("Y"));
$timestamp_stop = time();
break;
case "this_year":
$timestamp_start = mktime(0,0,0, 1, 1, date("Y"));
$timestamp_stop = time();
break;
case "last_week":
$timestamp_start = mktime(0, 0, 0, date("n"), date("j")-6, date("Y")) - ((date("N"))*3600*24);
$timestamp_stop = mktime(23, 59, 59, date("n"), date("j"), date("Y")) - ((date("N"))*3600*24);
break;
case "last_month":
$timestamp_start = mktime(0,0,0,date("n")-1, 1, date("Y"));
$timestamp_stop = mktime(23, 59, 59, date("n"), date("d")-date("j"), date("Y"));
break;
case "last_year":
$timestamp_start = mktime(0,0,0,1, 1, date("Y")-1);
$timestamp_stop = mktime(23,59,59,12,31, date("Y")-1);
break;
case "last_half_hour":
$timestamp_start = mktime(date("h"), date("i")-30, date("s"), date("n"), date("d"), date("Y"));
$timestamp_stop = time();
break;
case "last_hour":
$timestamp_start = mktime(date("h")-1, date("i"), date("s"), date("n"), date("d"), date("Y"));
$timestamp_stop = time();
break;
case "last_24_hours":
$timestamp_start = mktime(date("h"), date("i"), date("s"), date("n"), date("d")-1, date("Y"));
$timestamp_stop = time();
break;
case "last_seven_days":
$timestamp_start = mktime(date("h"), date("i"), date("s"), date("n"), date("d")-7, date("Y"));
$timestamp_stop = time();
break;
case "last_thirty_days":
$timestamp_start = mktime(date("h"), date("i"), date("s"), date("n"), date("d")-30, date("Y"));
$timestamp_stop = time();
break;
case "same_day_last_week":
$timestamp_start = mktime(0, 0, 0, date("n"), date("d")-7, date("Y"));
$timestamp_stop = mktime(23, 59, 59, date("n"), date("d")-7, date("Y"));
break;
case "same_week_last_year":
$timestamp_start = mktime(0, 0, 0, date("n"), date("j"), date("Y")-1)
- ( (date("N", mktime(0, 0, 0, date("n"), date("j"), date("Y") - 1 ) ) - 1 ) * 86400 );
$timestamp_stop = mktime(23, 59, 59, date("n"), date("j") + 7, date("Y")-1)
- ( (date("N", mktime(0, 0, 0, date("n"), date("j"), date("Y") - 1 ) ) - 1 ) * 86400 );
break;
case "same_month_last_year":
$timestamp_start = mktime(0,0,0, date("n"), 1, date("Y") - 1);
$timestamp_stop = mktime(23, 59, 59, date("n"), date("t"), date("Y") - 1);
break;
case "today":
default:
$timestamp_start = mktime(0,0,0, date("n"), date("d"), date("Y"));
$timestamp_stop = time();
break;
}



November 14th, 2008 - 23:51
Sweet man…thanks for the mention!
November 15th, 2008 - 00:02
Oh, yeah… “bonehead”, above, helped me out with a few of those mktime statements