views:

161

answers:

2

I'm using strtotime to create a timestamp from some ropey XML, but the timezone is incorrect.

How can I remove 5 hours from the time?

+8  A: 

If you know the timezone, you can set it with date_default_timezone_set()

When that's set, you can just do this in strtotime() as you know.

strtotime("-5 hours", $time);
Ólafur Waage
fast typer .. beat me to it by 23 sec :))
solomongaby
+1  A: 

You probably want something like:

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

This formats a string as a date and time, for the current time in seconds since epoch - 18000 seconds (five hours). You can adjust the number you are subtracting to adjust how many hours you're adjusting by. It might be more clear if you write the 18000 as (60*60*5).

strtotime() can have unexpected consequences with strings where ordering is different than the US. European notation (01/02/2009) would be read as January 2nd where it is meant to be February 1st. This is the only issue I can see with olafur's solution, other than that it is solid, but either this one or that one will work.

Brett Bender
I'm nit picking but 18000 is a "magic number", so you'll want to add $five_hours = 5 * 60 * 60; then put that in your code.
TravisO
I did specifically mention that it would be more clear to write it as (60*60*5) instead of 18,000. Did you even read the answer i gave beyond just the code sample?
Brett Bender