How to Convert Date or Time to any Timezone and Dateformat with PHP
Date manipulation is a common and extremely useful development task. It was never easy with PHP, until PHP5 and DateTime class.
There are many cases you need to convert dates. For example, if you develop an international web application, every user is in different timezone. Additionally, it is useful to store all dates in UTC (GMT), as it is the universal time. It is obvious that you need to convert dates from one timezone to another and from date format prefered by each user to dateformat you use to store dates (YmdHis is a good choice).
A solution could be the following simple function date_convert()
:
function date_convert($dt, $tz1, $df1, $tz2, $df2) { // create DateTime object $d = DateTime::createFromFormat($df1, $dt, new DateTimeZone($tz1)); // convert timezone $d->setTimeZone(new DateTimeZone($tz2)); // convert dateformat return $d->format($df2); }
Examples
Convert datetime 28/10/2013 02:00:00 in Europe/Athens to UTC timastamp:
date_convert('28/10/2013 02:00:00', 'Europe/Athens', 'd/m/Y H:i:s', 'UTC', 'YmdHis');
It returns: 20131028000000
Convert UTC timastamp 20131028000000 to Europe/Athens datetime with format d/m/Y H:i:s:
date_convert('20131028000000', 'UTC', 'YmdHis', 'Europe/Athens', 'd/m/Y H:i:s');
It returns: 28/10/2013 02:00:00
The complete version
Here is a more advanced syntax of this function, including error handling:
<?php /** * Convert a date(time) string to another format or timezone * * DateTime::createFromFormat requires PHP >= 5.3 * * @param string $dt * @param string $tz1 * @param string $df1 * @param string $tz2 * @param string $df2 * @return string */ function date_convert($dt, $tz1, $df1, $tz2, $df2) { $res = ''; if(!in_array($tz1, timezone_identifiers_list())) { // check source timezone trigger_error(__FUNCTION__ . ': Invalid source timezone ' . $tz1, E_USER_ERROR); } elseif(!in_array($tz2, timezone_identifiers_list())) { // check destination timezone trigger_error(__FUNCTION__ . ': Invalid destination timezone ' . $tz2, E_USER_ERROR); } else { // create DateTime object $d = DateTime::createFromFormat($df1, $dt, new DateTimeZone($tz1)); // check source datetime if($d && DateTime::getLastErrors()["warning_count"] == 0 && DateTime::getLastErrors()["error_count"] == 0) { // convert timezone $d->setTimeZone(new DateTimeZone($tz2)); // convert dateformat $res = $d->format($df2); } else { trigger_error(__FUNCTION__ . ': Invalid source datetime ' . $dt . ', ' . $df1, E_USER_ERROR); } } return $res; } ?>
REMARK: you might either use trigger_error or throw an Exception, according to your development style.
References
From PHP manual
- DateTime class (PHP 5 >= 5.2.0)
- DateTime::getLastErrors() (PHP 5 >= 5.3.0)
- DateTime::createFromFormat (PHP 5 >= 5.3.0)
- timezone_identifiers_list() (PHP 5 >= 5.2.0)
Your comments are welcomed!
This site actively encourages commenting on any post. Comments are not pre-moderated, but this community does not tolerate direct or indirect attacks, name-calling or insults. Please, read terms of use and Comment Policy at privacy policy.