Skip to content

Instantly share code, notes, and snippets.

@cristianobecker
Last active March 7, 2016 14:59
Show Gist options
  • Save cristianobecker/c11f52577b9037d0544e to your computer and use it in GitHub Desktop.
Save cristianobecker/c11f52577b9037d0544e to your computer and use it in GitHub Desktop.
Convert UTC date to a custom timezone in PHP
<?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