Tag Archives: DST

Non-procedural datetime conversions

Aren’t you tired of using  strtotime() and messing up timezones / DST now and then?

Here’s the OOP way, easy to understand and maintain! whoohoo!

Converting a UTC datetimestamp to your local date time.:

$my_utc_datetime = '11-14-2013 10:38:00'; // November 14th
$utc_datetime_object = DateTime::createFromFormat(
    'm-d-Y H:i:s',           // current format
    $my_utc_datetime,        // the datetimestamp
    new DateTimeZone('UTC')  // the origin timezone
);
// Now let's convert it to amsterdam time
$amsterdam_datetime = $utc_datetime_object;
$amsterdam_datetime->setTimeZone(new DateTimeZone('Europe/Amsterdam'));
echo $amsterdam_datetime->format('Y-m-d H:i:s'); // This will echo amsteram datetime

And the good news this will take DST into account!

No more daylight saving time problems!

PHP.net:

DateTime
DateTime::createFromFormat()

Tagged , , , ,