note 84350 added to function.date

From: Date: Thu, 10 Jul 2008 03:46:33 +0000
Subject: note 84350 added to function.date
Groups: php.notes 
Request: Send a blank email to php-notes+get-141346@lists.php.net to get a copy of this message
Doing $w-- for months ending on Sat won't hurt (i.e. if you're counting weeks as is the case below), but halocastle's code is perfectly fine as is and quite fast. He/she uses $w as a key for the $weeks array. "Halo" does this BEFORE $w++, so $w-- is superfluous as the loop has already ended. For May, 2008, I get 5 weeks as expected... Array ( [1] => Array ( [4] => 1 [5] => 2 [6] => 3 ) [2] => Array ( [0] => 4 [1] => 5 ------------OMITTED----------------- [4] => 22 [5] => 23 [6] => 24 ) [5] => Array ( [0] => 25 [1] => 26 [2] => 27 [3] => 28 [4] => 29 [5] => 30 [6] => 31 ) ) I guess the one pit-fall of the code is if you overlap months, say the following year, then $m-- makes perfect since...I think (haven't gotten that far...yet). I modified "Halo's" code to include months, too (this is from a snippet that produces a three month calendar, hence the outer $months loop, omitted here). $m = date('m'); $Y = date('Y'); // for() {months loop omitted $var_date = mktime(0, 0, 0, $m, 1, $Y); $month_name = date('F', $var_date); $months[$month_name]['DAYS'] = date('t', $var_date); $months[$month_name]['FIRST_DAY'] = date('w', $var_date); //} foreach($months as $month => $key) { $weeks = array(); for($i = 1, $j = $key['FIRST_DAY'], $w = 1;$i <= $key['DAYS'];$i++) { $weeks[$w][$j] = $i; $j++; if($j == 7) { $j = 0; $w++; } } $months[$month]['WEEKS'] = $weeks; } Enjoy! ---- Server IP: 209.41.74.194 Probable Submitter: 70.191.178.48 ---- Manual Page -- http://www.php.net/manual/en/function.date.php Edit -- https://master.php.net/note/edit/84350 Del: integrated -- https://master.php.net/note/delete/84350/integrated Del: useless -- https://master.php.net/note/delete/84350/useless Del: bad code -- https://master.php.net/note/delete/84350/bad+code Del: spam -- https://master.php.net/note/delete/84350/spam Del: non-english -- https://master.php.net/note/delete/84350/non-english Del: in docs -- https://master.php.net/note/delete/84350/in+docs Del: other reasons-- https://master.php.net/note/delete/84350 Reject -- https://master.php.net/note/reject/84350 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#141346) next »