Hello! Tell me please how to get the last number of weeks in a year?
+6
A:
Replace 2010 by the year you want and Europe/Berlin with your timezone:
<?php
date_default_timezone_set('Europe/Berlin');
echo gmdate("W", strtotime("31 December 2010"));
?>
You'll get one of the values 01, 52 or 53.
Just for fun:
<?php
date_default_timezone_set('Europe/Berlin');
for ($year = 1900; $year < 2100; $year++) {
echo $year . " => " .
gmdate("W", strtotime("31 December " . $year)) . "\n";
}
?>
The MYYN
2010-07-23 15:06:00
A:
If you were asking how to get the number of weeks left in the year, this would do it:
<?php
$year = date('Y');
$week_count = date('W', strtotime($year . '-12-31'));
if ($week_count == '01')
{
$week_count = date('W', strtotime($year . '-12-24'));
}
echo ($week_count - date('W'));
echo ' weeks left in ' . date('Y') . '!';
?>
Edit: Added logic to compensate for the '01' returned by date('W');
joshtronic
2010-07-23 15:37:52
That might not yield an expected result, because as stated in the question comments, the last week may be `01`. In such a case, your code would yield a negative number of weeks left in the year. You should programmatically convert the edge case `01` (by adding 1 to the next-to-last week).
Paul Lammertsma
2010-07-23 15:41:41
@paul I just edited the code, pretty sure that would compensate for the returned '01' correctly... on second thought, it would still bunk out when the current week returns 01 as well... sooo close ;)
joshtronic
2010-07-23 16:12:41
It's something along those lines, anyway. :)
Paul Lammertsma
2010-07-23 21:54:24