Strtotime date weird result

Given the following line: Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time)

in php, if I do strtotime on above and then convert it back to string, it seems like an hour.

echo $str_date,"  Vs ",date("c",strtotime($str_date));

It produces:

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

I understand that this is due to summer savings, but how to compensate for this?

+5
source share
4 answers

I understand that this is due to summer savings, but how to compensate for this?

Not using date()and strtotime(); class DateTimeis preferred.

$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

or in procedural style

$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

Also, if you want to use date()/ strtotime(), then as other answers and your own observations show, you need to be careful with the time zones used in the date string and your script.

+1
source

, ,
,
GMT, -

(GMT + 8)

php -r "echo date('r', strtotime('Fri Sep 02 2011 21:00:00 GMT+0100'));"
Sat, 03 Sep 2011 04:00:00 +0800

7 , GMT+8 - GMT+1 = 7

+3

PHP ? date.timezone? , PHP 5.3.6 Mac OS X:

$str_date   = 'Fri Sep 02 2011 21:00:00 GMT+0100  (GMT Daylight Time)';
echo $str_date,"  Vs ",date("c",strtotime($str_date));
// Fri Sep 02 2011 21:00:00 GMT+0100  (GMT Daylight Time)  Vs 1970-01-01T01:00:00+01:00

, Fri Sep 02 2011 21:00:00 GMT+0100 (GMT Daylight Time) /.

$str_date   = 'Fri Sep 02 2011 21:00:00 GMT+0100';
echo $str_date,"  Vs ",date("c",strtotime($str_date));
// Fri Sep 02 2011 21:00:00 GMT+0100  Vs 2011-09-02T22:00:00+02:00

, GMT + 2.

+1

, strtotime() SOAP: YY "-" MM "-" DD "T" HH ":" II ":" SS frac tzcorrection?

:

"2008-07-01T22:35:17.02", "2008-07-01T22:35:17.03+08:00"

- . http://www.php.net/manual/en/datetime.formats.compound.php

0

All Articles