开发者

How can I change timezones in a php date?

I have a date stored in a database in this format:

2011-02-23 13:00:00

I need to return it in ISO8601 format, but it needs to be set to a specific time zone (which is not necessarily the t开发者_高级运维ime zone of the server.) What I want to return is this:

2011-02-23T13:00:00-0600

Using this code:

echo date(DATE_ISO8601, strtotime("2011-02-23 13:00:00"));

I get this:

2011-02-23T13:00:00+0000

Is there any way to reset the time zone in the date or strtotime function, or do I need to strip off the 5 rightmost characters and concatenate the desired timezone stamp to the remaining date/time?

EDITED TO ADD: Although I did accept the solution below of using new DateTime and setting new DateTimeZone, I found an easier way if you don't need to keep resetting the time zone:

date_default_timezone_set('America/Chicago');
$startTime = date(DATE_ISO8601, strtotime("2011-02-23 13:00:00"));


You could use the DateTime class. Datetime objects can be initialized with a specific time zone, and easily transposed to others.

Modified from the manual:

$date = new DateTime('2011-02-23 13:00:00', new DateTimeZone('Pacific/Nauru'));
echo $date->format('c') . "\n";

$date->setTimezone( new DateTimeZone('Europe/Berlin'));
echo $date->format('c') . "\n";
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜