note 47230 added to function.mktime

From: Date: Sun, 07 Nov 2004 19:13:33 +0000
Subject: note 47230 added to function.mktime
Groups: php.notes 
Request: Send a blank email to php-notes+get-80234@lists.php.net to get a copy of this message
I found a bug in the code I posted yesterday. Date ("w") returns a zero for a sunday which has to be a 7 when we start counting on monday with a 1. Since 2003 and 2004 have januari 4 on a sunday it didn't appear to me that I corrected this in the $offset variable. For years that don't have jan 4 on a sunday the function returned bogus. The corrected one: <?PHP function datefromweeknr($year, $week, $day) { $jan04 = mktime (0,0,0,01,4, $year); // jan 04 always in week 1 $firstday = date("w", $jan04); // fetch day of week of jan 04 if($firstday == 0) { $firstday = 7; } $offset = ($week-1) * 7 - $firstday + $day; //calc number of days difference from jan 04 return strtotime('+' .$offset .' days', $jan04); } ?> ---- Manual Page -- http://www.php.net/manual/en/function.mktime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+47230 Delete -- http://master.php.net/manage/user-notes.php?action=delete+47230&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47230&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47230&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47230&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47230&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47230&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+47230&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#80234) next »