note 84145 modified in function.date by danbrown

From: Date: Sun, 17 May 2009 18:55:48 +0000
Subject: note 84145 modified in function.date by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-155188@lists.php.net to get a copy of this message
Weeks and days for any month/year combo: <?php $m = 2; // February $Y = 2008; // constants used here for legibility, use $vars for dynamicon... define('MONTH_DAYS',date('t', strtotime(date($m . '/01/' . $Y)))); // w:0->6 = Sun->Sat define('MONTH_FIRST_DAY',date('w', strtotime(date($m . '/01/' . $Y)))); for($i = 1, $j = MONTH_FIRST_DAY, $w = 1;$i <= MONTH_DAYS;$i++) { $week[$w][$j] = $i; $j++; if($j == 7) { $j = 0; $w++; } } ?> print_r($week): ----------------------- Array ( [1] => Array ( [5] => 1 [6] => 2 ) [2] => Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 [4] => 7 [5] => 8 [6] => 9 ) [3] => Array ( [0] => 10 [1] => 11 [2] => 12 [3] => 13 [4] => 14 [5] => 15 [6] => 16 ) [4] => Array ( [0] => 17 [1] => 18 [2] => 19 [3] => 20 [4] => 21 [5] => 22 [6] => 23 ) [5] => Array ( [0] => 24 [1] => 25 [2] => 26 [3] => 27 [4] => 28 [5] => 29 ) ) [EDIT BY danbrown AT php DOT net: In a note dated 03-JUL-08, (dmagick AT gmail DOT com) offered the following amendment to this note.] code as it doesn't take into account when a month finishes on a Saturday (eg May 2008). <?php $start_date = mktime(0, 0, 0,$start_month, 1, $start_year); $days_in_month = date('t', $start_date); $month_first_day = date('w', $start_date); $j = $month_first_day; $num_weeks = 1; for($i = 1; $i <= $days_in_month; $i++) { $j++; if($j == 7) { $j = 0; $num_weeks++; } } // if the last day of the month happens to be a Saturday, // take one off the number of weeks // because it was being added inside the for loop. if ($j == 0) { $num_weeks--; } ?> --was-- Weeks and days for any month/year combo: <?php $m = 2; // February $Y = 2008; // constants used here for legibility, use $vars for dynamicon... define('MONTH_DAYS',date('t', strtotime(date($m . '/01/' . $Y)))); // w:0->6 = Sun->Sat define('MONTH_FIRST_DAY',date('w', strtotime(date($m . '/01/' . $Y)))); for($i = 1, $j = MONTH_FIRST_DAY, $w = 1;$i <= MONTH_DAYS;$i++) { $week[$w][$j] = $i; $j++; if($j == 7) { $j = 0; $w++; } } ?> print_r($week): ----------------------- Array ( [1] => Array ( [5] => 1 [6] => 2 ) [2] => Array ( [0] => 3 [1] => 4 [2] => 5 [3] => 6 [4] => 7 [5] => 8 [6] => 9 ) [3] => Array ( [0] => 10 [1] => 11 [2] => 12 [3] => 13 [4] => 14 [5] => 15 [6] => 16 ) [4] => Array ( [0] => 17 [1] => 18 [2] => 19 [3] => 20 [4] => 21 [5] => 22 [6] => 23 ) [5] => Array ( [0] => 24 [1] => 25 [2] => 26 [3] => 27 [4] => 28 [5] => 29 ) ) http://php.net/manual/en/function.date.php

« previous php.notes (#155188) next »