简体   繁体   中英

Inconsistent strtotime conversions

When doing a conversion on a date using strtotime, I'm getting inconsistent results.

array[0] holds the date to be converted:

Original -> converted -> converted back

Array ([0] => 30.01.15) -> 1422576000 -> GMT: Fri, 30 Jan 2015 00:00:00 GMT
Array ([0] => 23.01.15) -> 1427925675 -> GMT: Wed, 01 Apr 2015 22:01:15 GMT

When using dashes, instead of dots, I get:

Array ([0] => 30-01-15) -> 1894665600 -> GMT: Tue, 15 Jan 2030 00:00:00 GMT
Array ([0] => 23-01-15) -> 1673740800 -> GMT: Sun, 15 Jan 2023 00:00:00 GMT

It works in the first instance, using dots, but in the second using dots I get todays date? Any ideas what might be causing this inconsistency?

According to PHP manual, date and time must be provided in supported date/time format: http://php.net/manual/en/datetime.formats.date.php

The "dot" notation does not seem to be supported for date values, so PHP tries to parse it as time, here's why you got this "strange" behaviour.

To better explain different behave: in first run 30 cannot be interpreted as hour value, so PHP tries with date parsing, in second run, 23 it's a reasonable hour value, so it goes for time.

The "Day, month and two digit year, with dots or tabs" format (dd [.\\t] mm "." yy) only works for the year values 61 (inclusive) to 99 (inclusive) - outside those years the time format "HH [.:] MM [.:] SS" has precedence

It appears that PHP is interpreting the dates in different formats.

Array ([0] => 30.01.15) -> 1422576000 -> GMT: Fri, 30 Jan 2015 00:00:00 GMT

Is reading the date as DD.MM.YY

Array ([0] => 30-01-15) -> 1894665600 -> GMT: Tue, 15 Jan 2030 00:00:00 GMT

Is reading the date as YY-MM-DD

Try changing this to:

Array ([0] => 15-01-30)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM