Viewed   79 times

I am storing dates in a MySQL database in datetime fields in UTC. I'm using PHP, and I've called date_timezone_set('UTC') so that all calls to date() (without timestamp) return the date in UTC.

I then have it so a given web site can select its timezone. Now I want dates to display in the site's timezone. So, if I have a date stored as '2009-04-01 15:36:13', it should display for a user in the PDT timezone (-7 hours) as '2009-04-01 08:36:13'.

What is the easiest (least code) method for doing this via PHP? So far all I've thought of is

date('Y-m-d H:i:s', strtotime($Site->getUTCOffset() . ' hours', strtotime(date($utcDate))));

Is there a shorter way?



Here's what we did with our servers. We set everything to use UTC, and we display in the user's time zone by converting from UTC on the fly. The code at the bottom of this post is an example of how to get this to work; you should confirm that it works in all cases with your setup (i.e. daylight savings, etc).

Configuring CentOS

  1. Edit /etc/sysconfig/clock and set ZONE to UTC
  2. ln -sf /usr/share/zoneinfo/UTC /etc/localtime

Configuring MySQL

  1. Import timezones into MySQL if necessary:

    mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

  2. Edit my.cnf and add the following within the [mysqld] section:

    default-time-zone = 'UTC'

PHP Code

Example usage:
  $unixtime = TimeUtil::dateTimeToTimestamp('2009-04-01 15:36:13');
  echo TimeUtil::UTCToPST("M d, Y - H:i:s", $unixtime);

// You should move this to your regular init method
date_default_timezone_set('UTC'); // make this match the server timezone

class TimeUtil {
    public static function timestampToDateTime($timestamp) {
        return gmdate('Y-m-d H:i:s', $timestamp);

    public static function dateTimeToTimestamp($dateTime) {
        // dateTimeToTimestamp expects MySQL format
        // If it gets a fully numeric value, we'll assume it's a timestamp
        // You can comment out this if block if you don't want this behavior
        if(is_numeric($dateTime)) {
            // You should probably log an error here
            return $dateTime;
        $date = new DateTime($dateTime); 
        $ret = $date->format('U');
        return ($ret < 0 ? 0 : $ret);

    public static function UTCToPST($format, $time) {
        $dst = intval(date("I", $time));
        $tzOffset = intval(date('Z', time()));
        return date($format, $time + $tzOffset - 28800 + $dst * 3600);

Monday, August 15, 2022


$file = '';
$file_headers = @get_headers($file);
if(!$file_headers || $file_headers[0] == 'HTTP/1.1 404 Not Found') {
    $exists = false;
else {
    $exists = true;

From here and right below the above post, there's a curl solution:

function url_exists($url) {
    return curl_init($url) !== false;
Tuesday, September 20, 2022

The DateTime class, here, might help (quoting):

Each component of date (e.g. year) is internally stored as 64-bit number so all imaginable dates (including negative years) are supported.

But note that:

  • It's only exists in PHP >= 5.2
  • And several methods only exist in PHP >= 5.3

So: beware of which methods you're using, if you're developping on PHP 5.3 and want your software to be compatible with PHP 5.2

Another solution (especially, if using Zend Framework in your application) would be the Zend_Date component (quoting):

Although PHP 5.2 docs state, "The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT," Zend_Date supports a nearly unlimited range, with the help of the BCMath extension

Saturday, October 29, 2022

According to the php.ini directives manual page, the timezone isn't set by default if you're using a version prior to 5.3. Then if you take a look at the page for date_default_timezone_get, it uses the following order for getting the timezone:

* Reading the timezone set using the date_default_timezone_set() function (if any)
* Reading the TZ environment variable (if non empty) (Prior to PHP 5.3.0)
* Reading the value of the date.timezone ini option (if set)
* Querying the host operating system (if supported and allowed by the OS)

UTC is the default if all of those fail, so that's probably a good starting point.

Friday, August 19, 2022
$src_tz = new DateTimeZone('America/Chicago');
$dest_tz = new DateTimeZone('America/New_York');

$dt = new DateTime("2000-01-01 12:00:00", $src_tz);

echo $dt->format('Y-m-d H:i:s');

Note that if the source time is UTC, you can change the one line to this:

$dt = new DateTime("2000-01-01 12:00:00 UTC");

Edit: Looks like you want to go to UTC. In that case, just use "UTC" as the parameter to the $dest_tz constructor, and use the original block of code. (And of course, you can omit the $src_tz parameter if it is the same as the default time zone.)

Tuesday, December 6, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :