Skip to content

Instantly share code, notes, and snippets.

@hirejordansmith
Last active August 29, 2015 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hirejordansmith/a12a86466fe8d40b9943 to your computer and use it in GitHub Desktop.
Save hirejordansmith/a12a86466fe8d40b9943 to your computer and use it in GitHub Desktop.
Change the default timezone being used for PHP DateTime Class
<?php
// This will output current time in EST
$timezone = 'EST';
$timestamp = time();
$datetime = new DateTime("now", new DateTimeZone($timezone)); //first argument "must" be a string
$datetime->setTimestamp($timestamp); //adjust the object to correct timestamp
echo '<b>Current Time: </b>' . $datetime->format('Ymd ga e');
echo '<br />';
// This will output current time + 24 hours in EST
$timezone = 'EST';
$timestamp = time()+86400;
$datetime = new DateTime("now", new DateTimeZone($timezone)); //first argument "must" be a string
$datetime->setTimestamp($timestamp); //adjust the object to correct timestamp
echo '<b>+24 hours:</b> ' . $datetime->format('Ymd ga e');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment