Skip to content

Instantly share code, notes, and snippets.

@peterlozano
Created July 3, 2014 09:21
Show Gist options
  • Save peterlozano/a4e9b7947953958a6964 to your computer and use it in GitHub Desktop.
Save peterlozano/a4e9b7947953958a6964 to your computer and use it in GitHub Desktop.
PHP: Convert dates between timezones, independent of php timezone configuration.
<?php
$from_value = '2011-10-04 00:00:00';
// Create a date using the original timezone.
$from_timezone = 'America/New_York';
$date = new DateTime($from_value, new DateTimeZone($from_timezone));
// Move the date to another timezone.
$to_timezone = 'UTC';
$date->setTimezone(new DateTimeZone($to_timezone));
// Get the destination value in the desired format.
$to_format = 'Y-m-d\TH:i:s'; //$to_format = 'Y-m-d H:i:s';
$to_value = $date->format($to_format);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment