Following on from my post last month about problems with PEAR daylight saving, here’s a quick example of using the PHP DateTime Class which was introduced recently (PHP 5.2).

Convert from UTC to Europe/London local time:

$time_object = new DateTime('2011-04-19 17:45', new DateTimeZone('UTC'));
$time_object->setTimezone(new DateTimeZone('Europe/London'));
$LondonDateTime = $time_object->format('Y-m-d H:i:s');

This will work with daylight saving too.

Here’s that example wrapped up in a handy time zone conversion function:

function convert_time_zone($date_time, $from_tz, $to_tz)
	$time_object = new DateTime($date_time, new DateTimeZone($from_tz));
	$time_object->setTimezone(new DateTimeZone($to_tz));
	return $time_object->format('Y-m-d H:i:s');

10 Responses to Time Zone Conversion Using PHP DateTime Class

  1. Marc says:

    Just wanted to say thanks for posting this. It helped me out.

  2. Ken says:

    Thanks! A real life saver!

  3. purab says:

    really helpful code. thanks.

  4. mallsop says:

    Works. Thanks!

  5. kiran says:

    Many thanks for this function :)

  6. Basant Mandal says:

    Hey, thanks A LOT!

  7. Dinesh Kumar Sahoo says:

    The above post TIME ZONE CONVERSION USING PHP DATETIME CLASS automatically considers day light savings and convert the time according to time zone give. Can you please write a function that converts time without considering day light saving …

  8. Shelim says:

    Many Many Thanks for your code.Really helpful code.

  9. Deano says:

    Thanks very much! this has helped me a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">