note 87901 modified in function.date by danbrown

From: Date: Mon, 29 Dec 2008 20:41:27 +0000
Subject: note 87901 modified in function.date by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-148655@lists.php.net to get a copy of this message
a while back i grabbed some code from this page that would generate the range of the current week, called getWeekRange... well this week spans 2 years, and it breaks the code... so i re-wrote it. enjoy <?php #-----------------------------------------------------------------# # getWeekRange: function getWeekRange(&$start_date, &$end_date, $week_offset=0) { $start_date = ''; $end_date = ''; // todays information list($yr, $mo, $da, $word) = explode('-', date("Y-m-d-l", strtotime("+".abs($week_offset)." week"))); // 0 (for Sunday) through 6 (for Saturday) $day_of_current_week = date('w', strtotime("+".abs($week_offset)." week")); // on sunday, they want last week through today. if ($day_of_current_week == 0){ $day_of_current_week = 7; } // calculate day offset to monday and sunday $days_to_monday = 1 - $day_of_current_week; $days_to_sunday = 7 - $day_of_current_week; // set the start and end dates for this week $start_date = date('Y-m-d', mktime(0, 0, 0, $mo, ($da + ($days_to_monday)), $yr)); $end_date = date('Y-m-d', mktime(0, 0, 0, $mo, ($da + ($days_to_sunday)), $yr)); } #-----------------------------------------------------------------# // test it echo "<pre>"; for ($i=0;$i<=52;$i++){ getWeekRange($start,$end,$i); echo "[".date("Y-m-d-l",strtotime($start))."] [".date("Y-m-d-l",strtotime($end))."]\n"; } echo "</pre>"; #-----------------------------------------------------------------# ?> outputs: [2008-12-29-Monday][2009-01-04-Sunday] [2009-01-05-Monday][2009-01-11-Sunday] [2009-01-12-Monday][2009-01-18-Sunday] --was-- a while back i grabbed some code from this page that would generate the range of the current week, called getWeekRange... well this week spans 2 years, and it breaks the code... so i re-wrote it. enjoy #-----------------------------------------------------------------# # getWeekRange: function getWeekRange(&$start_date, &$end_date, $week_offset=0) { $start_date = ''; $end_date = ''; // todays information list($yr, $mo, $da, $word) = explode('-', date("Y-m-d-l", strtotime("+".abs($week_offset)." week"))); // 0 (for Sunday) through 6 (for Saturday) $day_of_current_week = date('w', strtotime("+".abs($week_offset)." week")); // on sunday, they want last week through today. if ($day_of_current_week == 0){ $day_of_current_week = 7; } // calculate day offset to monday and sunday $days_to_monday = 1 - $day_of_current_week; $days_to_sunday = 7 - $day_of_current_week; // set the start and end dates for this week $start_date = date('Y-m-d', mktime(0, 0, 0, $mo, ($da + ($days_to_monday)), $yr)); $end_date = date('Y-m-d', mktime(0, 0, 0, $mo, ($da + ($days_to_sunday)), $yr)); } #-----------------------------------------------------------------# // test it echo "<pre>"; for ($i=0;$i<=52;$i++){ getWeekRange($start,$end,$i); echo "[".date("Y-m-d-l",strtotime($start))."] [".date("Y-m-d-l",strtotime($end))."]\n"; } echo "</pre>"; #-----------------------------------------------------------------# outputs: [2008-12-29-Monday][2009-01-04-Sunday] [2009-01-05-Monday][2009-01-11-Sunday] [2009-01-12-Monday][2009-01-18-Sunday] http://php.net/manual/en/function.date.php

« previous php.notes (#148655) next »