0

If date() formats a local time/date, and gmdate() formats a GMT/UTC date/time, why is this true?

date_default_timezone_set('America/Los_Angeles');
var_dump(date('U') === gmdate('U')); // true

On the command line:

$ php -r "date_default_timezone_set('America/Los_Angeles'); var_dump(date('U') === gmdate('U'));"
bool(true)

Why is the local timestamp equal to the UTC timestamp for different time zones?

SoTes
  • 3
  • 2
  • 1
    Timestamps are always in UTC. Formatted dates are localized according to the timezone setting. – mario Mar 22 '13 at 01:59

1 Answers1

4

Because timestamps are seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Notice the GMT? No matter what time zone you're in, the timestamp is relative to that time and timezone.

What you really want to do is:

$local = new DateTime();
$local->setTimeZone(new DateTimeZone('America/Los_Angeles'));
$gmt   = new DateTime();
$gmt->setTimeZone(new DateTimeZone('UTC'));
var_dump($local === $gmt);
John Conde
  • 217,595
  • 99
  • 455
  • 496