note 79694 deleted from function.date by derick
| From: | derick@php.net | Date: | Sun, 09 Dec 2007 13:36:01 +0000 |
| Subject: | note 79694 deleted from function.date by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-132993@lists.php.net to get a copy of this message | ||
Note Submitter: webmaster at nncoders dot nospam dot de
----
It has already been said that date() does only count up to week52 but it's really annoying.
date('W',strtotime('2007-12-30')) returns 52
date('W',strtotime('2007-12-31')) returns 1
but Monday of Week 1 of Year 2007 is the 01.01.2007
This is totally wrong.
Here's my solution:
<?php
function getweek($date='')
{
if ($date=='') { return false; }
$y = date('Y', strtotime($date));
$m = date('n', strtotime($date));
$d = date('j', strtotime($date));
#check-date
$cd = mktime(0,0,0,$m,$d,$y);
#last monday of the year (credits to jcwebb at dicoe dot com)
$lm = strtotime('last Monday',mktime(0,0,0,12,32,$y));
#If we are within the last week of the year
#and date('W') would return 1, return 53
#otherwise just do what date('W') does (there's no week 54)
if ((date('Ymd',$cd) >= date('Ymd',$lm)) && date('W',$cd)
== 1)
{ return 53; } else { return (int)date('W',$cd); }
}
echo getweek('2007-12-31');
?>