note 49457 modified in function.date by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 03:01:49 +0000 |
| Subject: | note 49457 modified in function.date by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142015@lists.php.net to get a copy of this message | ||
Calculus of weeks in a year.
Since there is date("W") many still seem to have a problem regarding how many weeks there
are in an year. Some rather complex solutions have been shown here.
It's defined, that a week which begins in december and ends in january the following year
belongs to the year where most of its days lie. Therefore a week with at least 4 days in december is
the last week of that year and a week with at least 4 days in january is the first week in the new
year.
This concludes, that the last week of a year always contains the 28th day of december. So if you
take date("W") on that day of a given year you always get the correct number of weeks for
that year.
The other end of that definition is that the 4th day of january always lies in the first week of a
year.
I hope this solves a lot of confusion.
(For those asking what all this fuzz about counting weeks is about: normally theres 52 weeks in a
year but sometimes its 53 weeks in a year)
I wrote it down as a function, but as this is rather trivial one might consider using the date(...)
only.
<?php
function weeks($year) {
return date("W",mktime(0,0,0,12,28,$year));
}
?>
--was--
Calculus of weeks in a year.
Since there is date("W") many still seem to have a problem regarding how many weeks there
are in an year. Some rather complex solutions have been shown here.
It's defined, that a week which begins in december and ends in january the following year
belongs to the year where most of its days lie. Therefore a week with at least 4 days in december is
the last week of that year and a week with at least 4 days in january is the first week in the new
year.
This concludes, that the last week of a year always contains the 28th day of december. So if you
take date("W") on that day of a given year you always get the correct number of weeks for
that year.
The other end of that definition is that the 4th day of january always lies in the first week of a
year.
I hope this solves a lot of confusion.
(For those asking what all this fuzz about counting weeks is about: normally theres 52 weeks in a
year but sometimes its 53 weeks in a year)
I wrote it down as a function, but as this is rather trivial one might consider using the date(...)
only.
function weeks($year) {
return date("W",mktime(0,0,0,12,28,$year));
}
http://php.net/manual/en/function.date.php