Question

I have some dates stored in gmt in a mysql db.
Say the clients timezone offset was -540; how would I correct the datetime coming from the database to reflect that? Is there a handy function I can put the date through, or am I going to need to add or subtract accordingly.

Était-ce utile?

La solution

Since you have the data items in GMT format, you will have to convert them to time stamps, using strtotime, more info here: http://php.net/manual/en/function.strtotime.php Once you have the time stamp, you can subtract the offset (540 times 60 seconds) from it, and then convert the result to a new date string, using date, more info here: http://php.net/manual/en/function.date.php

Here is one version of the code that could to this:

$dbValue = $row['date']; 
$timestamp = strtotime($dbValue) - (540*60);
$result = date("Y-m-d H:i:s", $timestamp);

Autres conseils

Perhaps this Snippet will help you:

$format = 'Y-m-d';
$obj = new DateTime();
$date = date($format, strtotime($obj->format('Y-m-d H:i:s')) - $obj->format('Z'));

See also http://www.php.net/manual/en/class.datetime.php for further readings.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top