note 43943 added to function.date
| From: | zach at crito dot org | Date: | Fri, 09 Jul 2004 13:44:08 +0000 |
| Subject: | note 43943 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-72922@lists.php.net to get a copy of this message | ||
riviere_gh at yahoo dot fr includes a useful date to week function, below. Seems to me that it needs
one minor change, or else it might correct the week incorrectly in some cases.
Change the function:
<?
function dateToSemaine($date){
$annee=date('Y',$date);
$datePremier="1/1/$annee";
$datePremier=strtotime($datePremier);
$joursNum=date('w',$datePremier)-2;
$nbJour=date('z',$date);
$semaine=intval(($nbJour+$joursNum)/7);
return $semaine+1;
}
?>
So that it looks like this:
<?
function dateToSemaine($date){
$annee=date('Y',$date);
$datePremier="1/1/$annee";
$datePremier=strtotime($datePremier);
$joursNum=date('w',$datePremier)-2;
$nbJour=date('z',$date)+1;
$semaine=intval(($nbJour+$joursNum)/7);
return $semaine+1;
}
?>
This makes sense, because the date function counts the day of year beginning from 0, so we have to
add one to line 6 so that the function recognizes day 1 as day 1 rather than day 0.
After making this change, the function can correctly return the number of weeks in January 2005.
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+43943
Delete -- http://master.php.net/manage/user-notes.php?action=delete+43943&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+43943&report=yes
Search -- http://master.php.net/manage/user-notes.php