Created
March 1, 2017 14:35
-
-
Save imankulov/b4266bb8aa729147874180ba164ef270 to your computer and use it in GitHub Desktop.
Timezone magic to find the boundaries of the day
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
$utc = new DateTimeZone('UTC'); | |
$user_tz = new DateTimeZone('Europe/Moscow'); | |
// current time in UTC | |
date_default_timezone_set('UTC'); | |
$datetime = new DateTime(); | |
// convert current time to user's local time | |
$datetime->setTimezone($user_tz); | |
echo "Now in Moscow: " . $datetime->format("Y-m-d H:i:s") . " Europe/Moscow\n"; | |
// Start of the day in Moscow, converted back to UTC | |
$datetime->setTime(0, 0, 0); | |
$datetime->setTimezone($utc); | |
echo "User day starts: " . $datetime->format("Y-m-d H:i:s") . " UTC\n"; | |
$datetime->add(new DateInterval("P1D")); | |
echo "User day ends: " . $datetime->format("Y-m-d H:i:s") . " UTC\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment