mad4it Posted December 17, 2008 Share Posted December 17, 2008 Hi, I need to be able to perform a loop around a set of dates. The code within the loop is no problem however I'm having problems in working out the loop code. Effectively I need to be able to loop, one day at a time, from Monday of this week to Sunday, ie if I was running the code today I would be interested in the dates of 15-12-2008 to 21-12-2008 inclusive. I've already written code to ascertain which day of the week we are currently on and how many days it is away from Monday, ie so at the moment by variable $week_day is 2 - but how can I then use this to come up with Mondays date? Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/137444-date-problem/ Share on other sites More sharing options...
twm Posted December 17, 2008 Share Posted December 17, 2008 //From the php.net strtotime entry user comments: ################ # Returns date of the Monday for given number of Iso Week(1..53) # and year. Output is UNIX-Timestamp <?php function get_monday ($week, $year="") { $first_date = strtotime("1 January ".($year ? $year : date("Y"))); $w_day = date("w", $first_date); $d_week = 0; switch($w_day) { case 1: $monday = $first_date; break; case 2: case 3: case 4: $d_week = 604800; default:$monday = strtotime("Monday", $first_date)-$d_week; }; $plus_week = "+".($week-1)." week"; return (strtotime($plus_week, $monday)); }; # to use it call: date("m/d/Y", get_monday($someweek, $someyear)); ?> Link to comment https://forums.phpfreaks.com/topic/137444-date-problem/#findComment-718250 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.