note 107606 added to function.date
| From: | gerben at gerbenwijnja dot nl | Date: | Tue, 21 Feb 2012 17:54:35 +0000 |
| Subject: | note 107606 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186258@lists.php.net to get a copy of this message | ||
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;
}
?>
----
Server IP: 91.199.167.223
Probable Submitter: 80.57.149.71
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/107606
Del: integrated -- https://master.php.net/note/delete/107606/integrated
Del: useless -- https://master.php.net/note/delete/107606/useless
Del: bad code -- https://master.php.net/note/delete/107606/bad+code
Del: spam -- https://master.php.net/note/delete/107606/spam
Del: non-english -- https://master.php.net/note/delete/107606/non-english
Del: in docs -- https://master.php.net/note/delete/107606/in+docs
Del: other reasons-- https://master.php.net/note/delete/107606
Reject -- https://master.php.net/note/reject/107606
Search -- https://master.php.net/manage/user-notes.php