Reputation: 101
I don't understand why this is a valid date
strtotime("1920-09k-12") // -1556877600 // I expect false
I see that adding a char after the month or day is a valid date.
strtotime("1920-09-12d") // -1555905600 // I expect false
strtotime("1920-09n-12") // -1556838000 // I expect false
Instead
strtotime("1920-09k-12k") // false
strtotime("1920r-09-12") // false
strtotime("1920-09-12") // -1555862400
Is this the expected behaviour?
I use Laravel and the strtotime function is used for validate date in the framework but when go to save a record in DB with a "false positive" date a QueryException is raised. I resolved the problem with a custom validation but i'm curious to know why strtotime has this behaviour.
Upvotes: 1
Views: 154
Reputation: 1447
This may not fully answer your question, but examining the results of date_parse()
for your sample dates, it appears that the first letter in the string is interpreted as the timezone, and the remainder of the string as well, which either causes a warning or an error of "Double timezone specification"
, and in the case of an error, the DateTime
cannot be created.
For example:
date_parse("1920-09-12d")
Array
(
[year] => 1920
[month] => 9
[day] => 12
[hour] =>
[minute] =>
[second] =>
[fraction] =>
[warning_count] => 0
[warnings] => Array
(
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] => 1
[zone_type] => 2
[zone] => -240
[is_dst] =>
[tz_abbr] => D
)
Notice the timezone "D"
date_parse("1920-09n-12")
Array
(
[year] => 1920
[month] => 9
[day] => 1
[hour] =>
[minute] =>
[second] =>
[fraction] =>
[warning_count] => 1
[warnings] => Array
(
[8] => Double timezone specification
)
[error_count] => 0
[errors] => Array
(
)
[is_localtime] => 1
[zone_type] => 2
[zone] => 60
[is_dst] =>
[tz_abbr] => N
)
Notice the timezone "N", furthermore the 12 is not interpreted as the day of month, but rather I suspect "-12" is interpreted as an additional timezone specification, hence the warning.
date_parse("1920r-09-12")
Array
(
[year] =>
[month] =>
[day] =>
[hour] => 19
[minute] => 20
[second] => 0
[fraction] =>
[warning_count] => 1
[warnings] => Array
(
[5] => Double timezone specification
)
[error_count] => 1
[errors] => Array
(
[8] => Double timezone specification
)
[is_localtime] => 1
[zone_type] => 2
[zone] => 300
[is_dst] =>
[tz_abbr] => R
)
Notice the timezone "R", furthermore no date is parsed, rather 1920
is interpreted as the time 19:20:00
, and I suspect the remainder of the string is interpreted as 2 timezones, "-09" & "-12", causing the error "Double timezone specification".
Upvotes: 1