Instantly share code, notes, and snippets.

Embed
What would you like to do?
PHP: Converting DateTime between timezones
<?php
// now
$date = date_create('2010-12-31 22:00:01', timezone_open('Europe/Amsterdam'));
// Buenos Aires
date_timezone_set($date, timezone_open('America/Argentina/Buenos_Aires'));
echo $date->format('Y-m-d H:i:s');
// 2010-12-31 18:00:01
// UTC
date_timezone_set($date, timezone_open('UTC'));
echo $date->format('Y-m-d H:i:s');
// 2010-12-31 20:00:01
// London
date_timezone_set($date, timezone_open('Europe/London'));
echo $date->format('Y-m-d H:i:s');
// 2010-12-31 21:00:01
// Maldives
date_timezone_set($date, timezone_open('Indian/Maldives'));
echo $date->format('Y-m-d H:i:s');
// 2011-01-01 02:00:01
// Melbourne
date_timezone_set($date, timezone_open('Australia/Melbourne'));
echo $date->format('Y-m-d H:i:s');
// 2011-01-01 08:00:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment