How to I accurately get current UTC time via strtotime?
Asked Answered
K

4

7

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.

Kaiser answered 23/9, 2009 at 4:14 Comment(3)
Just to check, are the hosting providers in the same time zone as you are? You might try checking the output of date("T") to see.Asco
@Tom: That would work if the OP can assume his provider is in a certain region. The problem with date("T") is that for example "EST" might by the North American EST or the Australian EST.Eyeball
I was just fishing for information. See my answer below for a solution that would appear to work in all cases. Your point about time zone abrev. is well taken and something that I've never had to deal with since all my servers are in the U.S.Asco
A
8

This seems to work for me. Of course, you'll need to test it on PHP 4 since all of my servers have PHP 5, but the manual claims this should work for PHP 4.

$t = time();
$x = $t+date("Z",$t);
echo strftime("%B %d, %Y @ %H:%M:%S UTC", $x);

First time around, I forgot that the date could change between the call to time() and date().

Asco answered 23/9, 2009 at 4:46 Comment(3)
Much better then using date("T"). Well worth a +1.Eyeball
Thanks, and I had given you a +1 for the comment above.Asco
I was just browsing through the PHP Manual and ran accross gmdate. So, something like this: echo gmdate("H:i:s")." UTC"; should work as well.Asco
R
13
$time = new DateTime('now', new DateTimeZone('UTC'));
echo $time->format('F j, Y H:i:s');
Rend answered 23/9, 2009 at 12:26 Comment(3)
Thanks for your answer. Although this doesn't work for PHP 4.4.9, it does work fine on the other PHP 5.2.Kaiser
Sorry, apparently my reading comprehension was off when I read your question. Glad to have helped anyway.Rend
but was nevertheless valuable for other peoplCarlile
A
8

This seems to work for me. Of course, you'll need to test it on PHP 4 since all of my servers have PHP 5, but the manual claims this should work for PHP 4.

$t = time();
$x = $t+date("Z",$t);
echo strftime("%B %d, %Y @ %H:%M:%S UTC", $x);

First time around, I forgot that the date could change between the call to time() and date().

Asco answered 23/9, 2009 at 4:46 Comment(3)
Much better then using date("T"). Well worth a +1.Eyeball
Thanks, and I had given you a +1 for the comment above.Asco
I was just browsing through the PHP Manual and ran accross gmdate. So, something like this: echo gmdate("H:i:s")." UTC"; should work as well.Asco
F
0
$utcTtime = gmmktime();
$unixTimestamp = time();

gmmktime: Get Unix timestamp for a GMT date

Flossie answered 23/9, 2009 at 12:31 Comment(0)
R
0

Does this work for php 4.4.9?

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

or if you want it for a specific date:

$time = strtotime('January 1, 2000 UTC');
if($time){
    echo gmdate('Y-m-d H:i:s', $time);
}
Rivard answered 1/10, 2013 at 9:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.