How to I accurately get current UTC time via strtotime?

Dave picture Dave · Sep 23, 2009 · Viewed 9.7k times · Source

In PHP, how do I get the current time, in UTC, without hard coding knowledge of where my hosting provider is?

For example, I tried the following:

time() + strtotime('January 1, 2000')-strtotime('January 1, 2000 UTC')

and find that it reports a time that is one hour ahead of actual UTC time. I tried this on two different hosting providers in two different time zones with the same results.

Is there a reliable (and, hopefully, cleaner) way to accurately get the UTC time?

I am limited to PHP 4.4.9 so I cannot use the new timezone stuff added to PHP5.

Thanks, in advance.

Answer

GZipp picture GZipp · Sep 23, 2009
$time = new DateTime('now', new DateTimeZone('UTC'));
echo $time->format('F j, Y H:i:s');