Skip to content

Instantly share code, notes, and snippets.

@imankulov
Created March 1, 2017 14:35
Show Gist options
  • Save imankulov/b4266bb8aa729147874180ba164ef270 to your computer and use it in GitHub Desktop.
Save imankulov/b4266bb8aa729147874180ba164ef270 to your computer and use it in GitHub Desktop.
Timezone magic to find the boundaries of the day
<?
$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