note 107606 deleted from function.date by peehaa
| From: | peehaa@php.net | Date: | Fri, 06 Jan 2017 15:57:40 +0000 |
| Subject: | note 107606 deleted from function.date by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208714@lists.php.net to get a copy of this message | ||
Note Submitter: gerben at gerbenwijnja dot nl
----
I use the function below to calculate the Unix timestamp of the start of a week. It includes a
boolean flag to request a GMT offset instead of the current locale setting.
<?php
function getWeekOffsetTimestamp($year, $week, $useGmt = false) {
if ($useGmt) {
// Backup timezone and set to GMT
$timezoneSettingBackup = date_default_timezone_get();
date_default_timezone_set("GMT");
}
// According to ISO-8601, January 4th is always in week 1
$halfwayTheWeek = strtotime($year."0104 +".($week - 1)." weeks");
// Subtract days to Monday
$dayOfTheWeek = date("N", $halfwayTheWeek);
$daysToSubtract = $dayOfTheWeek - 1;
// Calculate the week's timestamp
$unixTimestamp = strtotime("-$daysToSubtract day", $halfwayTheWeek);
if ($useGmt) {
// Reset timezone to backup
date_default_timezone_set($timezoneSettingBackup);
}
return $unixTimestamp;
}
?>