Click to See Complete Forum and Search --> : Current week range


Horizon88
02-04-2008, 01:23 PM
Hey. This is a function I put together to return timestamps for the first and last day of the week given to it. It seems to work correctly, so if anyone feels like playing with it, suggestions and/or improvements are very welcome.


/*
* get_week_range accepts numeric $month, $day, and $year values.
* It will find the first sunday and the last saturday of the week for the
* given day, and return them as YYYY-MM-DD HH:MM:SS timestamps
*
* @param month: numeric value of the month (01 - 12)
* @param day : numeric value of the day (01 - 31)
* @param year : four-digit value of the year (2008)
* @return : array('first' => sunday of week, 'last' => saturday of week);
*/
function get_week_range($day='', $month='', $year='') {
// default empties to current values
if (empty($day)) $day = date('d');
if (empty($month)) $month = date('m');
if (empty($year)) $year = date('Y');
// do some figurin'
$weekday = date('w', mktime(0,0,0,$month, $day, $year));
$sunday = $day - $weekday;
$start_week = date('Y-m-d H:i:00', mktime(0,0,0,$month, $sunday, $year));
$end_week = date('Y-m-d H:i:00', mktime(0,0,0,$month, $sunday+6, $year));
if (!empty($start_week) && !empty($end_week)) {
return array('first'=>$start_week, 'last'=>$end_week);
}
// otherwise there was an error :'(
return false;
}

halojoy
03-14-2008, 01:53 AM
;)
hello partner helper

i wonder what you use it for
it is a very special feature

have you added some sort of calender to your website?

return value is an array in this format:
'Y-m-d H:i:00'array('first'=>$start_week, 'last'=>$end_week)so how would you use that result, horizon88?

php has got very good coverage of time date functions
so i guess there shouldnt be any problems with your script

bet you are glad to see 'a halojoy post' again
but how about laserlight .....

Regards
form Sweden (still lots of snow here outside my window)
... but spring will come .. sure as always .. every year as been done for 1.000s of years

in seasons we can trust .. but people are like people are
--- another instant quotation by halojoy

NogDog
03-14-2008, 02:11 AM
Maybe you could take it to the next level and define a class that would generate a "week" object for the specified date with variables/methods for getting each day of that week as a UNIX time, a GMT date string, or a local time date string?

PS: Or, of course, you could use the PEAR Date and/or Calendar classes? :)

Horizon88
03-14-2008, 04:29 AM
It was something I wrote at work, halojoy - I've been working on an events calendar there and needed a function that'd figure out the range of the week passed so they could display events by week.

NogDog: I just use it for SQL stuff, so it didn't need to be that in depth :P

Halojoy: To use the result I put it into an SQL query:

$sql = 'SELECT * FROM events WHERE event_time >= \''.$range['first'].'\' AND event_time <= \''.$range['last'].'\';';

To return events that are within that date range.