문제

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.

도움이 되었습니까?

해결책

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);

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top