note 69158 added to function.strtotime

From: Date: Thu, 24 Aug 2006 22:44:16 +0000
Subject: note 69158 added to function.strtotime
Groups: php.notes 
Request: Send a blank email to php-notes+get-116291@lists.php.net to get a copy of this message
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); } ---- Server IP: 193.27.201.10 (proxied: 193.27.201.10 1.0 www1.shellkonto.se:3128 (squid/2.5.STABLE4)) Probable Submitter: 213.113.151.132 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.strtotime.php Edit -- https://master.php.net/note/edit/69158 Del: integrated -- https://master.php.net/note/delete/69158/integrated Del: useless -- https://master.php.net/note/delete/69158/useless Del: bad code -- https://master.php.net/note/delete/69158/bad+code Del: spam -- https://master.php.net/note/delete/69158/spam Del: non-english -- https://master.php.net/note/delete/69158/non-english Del: in docs -- https://master.php.net/note/delete/69158/in+docs Del: other reasons-- https://master.php.net/note/delete/69158 Reject -- https://master.php.net/note/reject/69158 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#116291) next »