views:

1073

answers:

4

I am in need of an easy way to convert a date time stamp to UTC (from whatever timezone the server is in) HOPEFULLY without using any libraries.

+3  A: 

Use strtotime to generate a timestamp from the given string (interpreted as local time) and use gmdate to get it as a formatted UTC date back.

poke
+1  A: 

http://php.net/manual/en/function.strtotime.php or if you need to not use a string but time components instead, then http://us.php.net/manual/en/function.mktime.php

psychotik
A: 

Try the getTimezone and setTimezone, see the example

(But this does use a Class)

UPDATE:

Without any classes you could try something like this:

$the_date = strtotime("2010-01-19 00:00:00");
echo(date_default_timezone_get() . "<br />");
echo(date("Y-d-mTG:i:sz",$the_date) . "<br />");
echo(date_default_timezone_set("UTC") . "<br />");
echo(date("Y-d-mTG:i:sz", $the_date) . "<br />");

NOTE: You might need to set the timezone back to the original as well

Phill Pafford
You should just use `gmdate` instead of changing the default timezone to UTC. Because `gmdate` automatically uses UTC.
poke
-1 because you don't want to change the global default timezone for this. It just asks for unintended side effects.
WardB
A: 

If you have a date in this format YYYY-MM-HH dd:mm:ss, you can actually trick php by adding a UTC at the end of your "datetime string" and use strtotime to convert it.

date_default_timezone_set('Europe/Stockholm');
print date('Y-m-d H:i:s',strtotime("2009-01-01 12:00"." UTC"))."\n";
print date('Y-m-d H:i:s',strtotime("2009-06-01 12:00"." UTC"))."\n";

This will print this:

2009-01-01 13:00:00
2009-06-01 14:00:00

And as you can see it takes care of the daylight savings time problem as well.

A little strange way to solve it.... :)

Johan