note 69158 deleted from function.strtotime by derick

From: Date: Sun, 08 Apr 2007 13:00:16 +0000
Subject: note 69158 deleted from function.strtotime by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-125209@lists.php.net to get a copy of this message
Note Submitter: ---- My users want to skip to a certain week and I have looked in vain for a function which would give me dates for a specified week, or even the first day of a certain week. The function below is perhaps overly complicated, partly because my provider runs pre PHP 5.1.0. An ISO week starts with Monday, hence the need for day conversion. // ****************************************************** // Function that returns the dates for each day in a week // ****************************************************** function week_dates($week,$year) { $week_dates = array(); // Get timestamp of first week of the year $first_day = mktime(12,0,0,1,1,$year); $first_week = date("W",$first_day); if ($first_week > 1) { $first_day = strtotime("+1 week",$first_day); // skip to next if year does not begin with week 1 } // Get timestamp of the week $timestamp = strtotime("+$week week",$first_day); // Adjust to Monday of that week $what_day = date("w",$timestamp); // I wanted to do "N" but only version 4.3.9 is installed :-( if ($what_day==0) { // actually Sunday, last day of the week. FIX; $timestamp = strtotime("-6 days",$timestamp); } elseif ($what_day > 1) { $what_day--; $timestamp = strtotime("-$what_day days",$timestamp); } $week_dates[1] = date("Y-m-d",$timestamp); // Monday $week_dates[2] = date("Y-m-d",strtotime("+1 day",$timestamp)); // Tuesday $week_dates[3] = date("Y-m-d",strtotime("+2 day",$timestamp)); // Wednesday $week_dates[4] = date("Y-m-d",strtotime("+3 day",$timestamp)); // Thursday $week_dates[5] = date("Y-m-d",strtotime("+4 day",$timestamp)); // Friday $week_dates[6] = date("Y-m-d",strtotime("+5 day",$timestamp)); // Saturday $week_dates[7] = date("Y-m-d",strtotime("+6 day",$timestamp)); // Sunday return($week_dates); }

« previous php.notes (#125209) next »