Osman
Osman

Reputation: 1771

php date offset

This is my issue, i have a time stamp coming from php, the servers time is 3 hours off from mine. I have offset the time by three hours but quickly realized that at 2 am the date part of the time stamp reads the day before date (day and month) and that does not change till 3 am. This is an issue because the date is important and i need it to be accurate. I have tried the timezone change but cant seem to get it to work. I live in ohio so thats the time i need and the timezone the server is in is three hours behind. So one of two things can help me, a timezone change that works or offsetting the day by 3h, not only the time. Here is my current code:

 $timechange = mktime(date("g")+3, date("i"), 0, date("m"), date("d"), date("y"));

 $date = date("D, d M Y g:i",$timechange);

Upvotes: 0

Views: 801

Answers (2)

Marc B
Marc B

Reputation: 360672

$now = new DateTime('now', new DateTimeZone('America/Ohio')); // whatever your TZ's name happens to be

$now->setTimeZone('America/ServerTZ'); // reset to your server's TZ

$datestr = $now->format('D, d M Y g:i'); // get TZ's time as a nice string

By doing the setTimeZone, you affect the OUTPUT of the function - internally the timestamp is unchanged.

Upvotes: 4

simshaun
simshaun

Reputation: 21466

Instead of adjusting the times yourself, try just setting the timezone beforehand.

See http://php.net/manual/en/function.date-default-timezone-set.php

Upvotes: 1

Related Questions