Last active
March 7, 2016 14:59
-
-
Save cristianobecker/c11f52577b9037d0544e to your computer and use it in GitHub Desktop.
Convert UTC date to a custom timezone in PHP
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
<?php | |
function convert_to_timezone($date, $timezone, $format = 'Y-m-d H:i:s') { | |
$object = date_create_from_format($format, $date, timezone_open('UTC')); | |
date_timezone_set($object, timezone_open($timezone)); | |
return $object; | |
} | |
function convert_to_timezone_string($date, $timezone, $format = 'Y-m-d H:i:s') { | |
return date_format(convert_to_timezone($date, $timezone, $format), $format); | |
} | |
$date = '2016-02-10 19:25:00'; | |
echo convert_to_timezone_string($date, 'America/Sao_Paulo') . "\n"; | |
echo convert_to_timezone_string($date, 'America/Los_Angeles') . "\n"; | |
echo convert_to_timezone_string($date, 'Europe/London') . "\n"; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment