Setting timezone to UTC (0) in PHP

Andres SK picture Andres SK · Sep 28, 2011 · Viewed 186.4k times · Source

Why does this work?

date_default_timezone_set('Australia/Currie');

But this doesn't seem to take any effect at all?

date_default_timezone_set('UTC');

This value doesn't change when setting the timezone to UTC:

echo date('Y-m-d H:i:s', time());

I'm using php 5.2.13, and the timezone of my server is:

$server_tz = date_default_timezone_get();
echo $server_tz; //outputs 'America/Guayaquil'

This is the original code:

echo time() . "<br>\n";
date_default_timezone_set('UTC');
echo time() . "<br>\n";

Output:

1317235130
1317235130

Answer

Herbert picture Herbert · Sep 28, 2011

The problem is that you're displaying time(), which is a UNIX timestamp based on GMT/UTC. That’s why it doesn’t change. date() on the other hand, formats the time based on that timestamp.

A timestamp is the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

echo date('Y-m-d H:i:s T', time()) . "<br>\n";
date_default_timezone_set('UTC');
echo date('Y-m-d H:i:s T', time()) . "<br>\n";