Skip to content

Instantly share code, notes, and snippets.

@thlor
Created October 18, 2016 11:19
Show Gist options
  • Save thlor/aaffffd65458a5bfb418ae53149b649b to your computer and use it in GitHub Desktop.
Save thlor/aaffffd65458a5bfb418ae53149b649b to your computer and use it in GitHub Desktop.
new \DateTime() returns an object where the timezone is already set to the user's time zone.
<?php
$time = new \DateTime();
$default = $time->format('d.m.Y H:i');
// 18.10.2016 12:17
$user_timezone = drupal_get_user_timezone();
$timezone = new \DateTimeZone($user_timezone);
$time->setTimezone($timezone);
$with_user_timezone = $time->format('d.m.Y H:i');
// 18.10.2016 12:17
$timezone = new \DateTimeZone('UTC');
$time->setTimezone($timezone);
$with_utc_timezone = $time->format('d.m.Y H:i');
// 18.10.2016 11:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment