note 26444 deleted from function.strftime by derick
| From: | derick@php.net | Date: | Sun, 14 Aug 2005 12:02:33 +0000 |
| Subject: | note 26444 deleted from function.strftime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-93619@lists.php.net to get a copy of this message | ||
Note Submitter: matt at crx4u dot com
----
Here's a pair of functions to convert a date to an ISO 8601 year/week and vice versa.
function yearweek($date="") {
if (!$date) $date = mktime();
$yearweek = strftime("%G%V", $date);
return $yearweek;
}
function dateEndYearweek($yearweek) {
$weekEndDay = 0; // 0 = Sunday.
$year = (int) substr($yearweek, 0, 4);
$week = (int) substr($yearweek, -2, 2);
// We know that the 4th Jan is always in week 01, 11th Jan in week 02, ...
// so day (4 + (w-1)*7) is in week w.
$dayOfYear = 4 + (($week - 1) * 7);
$date = mktime(0, 0, 0, 1, $dayOfYear, $year);
// Find the last day of this week.
$dayOfWeek = date("w", $date);
$daysToAdd = ($weekEndDay - $dayOfWeek + 7) % 7;
$date += $daysToAdd * 24*60*60;
return $date;
}