开发者

PHP Time Zone Adjustment

I have a time that is being sent to me in UTC time, but I want to adjust it so its the outer edges of the days on East Coast time (EST/EDT). That is, I 开发者_运维技巧want the user to be able to enter in EDT/EST centric dates, and have it query with the UTC correct dates.

$start_date and $end_date are MM/DD/YYYY formatted dates passed via a GET variable.

 $start_date = date('Y-m-d 00:00:00', $start_date);
 $end_date = date('Y-m-d 23:59:59', $end_date);

These dates are for use in a database query, whose times are stored in UTC time.

So, I want the start date to be the day before at 8pm or 7pm, depending on daylight savings, and then to end at 7:59:59 or 6:59:59, again depending on daylight savings.

How would I do that?


If you know what timezone the data is coming from, you can do something like this:

$userTimezone = new DateTimeZone('America/New_York');
$systemTimezone = new DateTimeZone('Europe/London');

$ts = new DateTime($datetime, $userTimezone);
$ts->setTimezone($systemTimezone);

return $ts->format('Y-m-d H:i:s');

Where $datetime is a valid date format according to thse rules - http://www.php.net/manual/en/datetime.construct.php

This is the core of what we do in web2project - http://web2project.net/ - to get a datetime in the users' selected timezone but store it in GMT/UTC.


In MySQL you can do the following:

SELECT * FROM tablename WHERE date = DATE_SUB('2010-06-17 00:00:00',INTERVAL 4 HOUR);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜