strtotime日期奇怪的结果

Kev*_*haw 5 php date

给定以下字符串日期: Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time)

在PHP中,如果我在上面做了strtotime,然后将其转换回字符串日期,它似乎获得了一个小时.

echo $str_date,"  Vs ",date("c",strtotime($str_date));
Run Code Online (Sandbox Code Playgroud)

生产:

Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time) VS 2011-09-02T22:00:00+01:00

我意识到这与夏令时有关,但是如何弥补这一点呢?

sal*_*the 1

我意识到这与夏令时有关,但如何弥补这一点呢?

不使用date()and strtotime();班级DateTime是首选。

$str_date = 'Fri Sep 02 2011 21:00:00 GMT+0100';
$datetime = new DateTime($str_date);
echo $datetime->format('c'); // 2011-09-02T21:00:00+01:00
Run Code Online (Sandbox Code Playgroud)

或者以程序风格

$str_date = 'Fri Sep 02 2011 21:00:00 GMT+0100';
echo date_format(date_create($str_date), 'c'); // 2011-09-02T21:00:00+01:00
Run Code Online (Sandbox Code Playgroud)

另外:如果您希望仍然使用date()/ strtotime()then,正如其他答案和您自己的观察结果所示,您需要小心日期字符串和脚本中使用的时区。