Why do strtotime(' ') and strtotime('.') return a timestamp?
Asked Answered
C

1

7

After some messing around with strtotime() in PHP I noticed that it gives a valid timestamp back when you pass in spaces and or dots.

var_dump(strtotime(" "));
var_dump(strtotime("."));
var_dump(strtotime(". .. .. .. .... .. . .. ."));

produces:

int 1443009652
int 1443009652
int 1443009652

Why does PHP see this as valid?

Capriccioso answered 23/9, 2015 at 12:1 Comment(11)
You're passing in strings....what would you expect it to return?Wriggler
Probably because it tries to parse so many potential strings like "next Thursday", "+3 days", "now", "first Tuesday of last month", "23 September 2015", etc that it simply defaults to current date/time if it can't make sense of the string, rather writing an error to outputSaith
Hey, it works with commas too!Padnag
Interesting question: Why are you doing that?Homeopathy
It should return 'false' on something it can't parse it as a date/time according to the documentation.Capriccioso
Then raise a bug if you think that this is incorrect behaviourSaith
it takes default date and time and return a timestampCorrupt
Seriously, what the hell is this: ',. ., ,. .,1 week ,. ., 2 week,. .,3 week'Padnag
Yes, but why? strotime("bogus"); returns FALSE as expected. I would expect that ". . . ... .. " would return FALSE too as there is no way I can see that it is parsable as something close to a date/timeCapriccioso
Perhaps because dots, commas, spaces, slashes, dashes, etc ae all typically found as separators in date formats that can be passed to strtotime() whereas strtotime("bogus") is all purely letters.... but without wading through the PHP source cde, you're not going to get an answer to why, only speculationSaith
Thanks. I'll file a bug and see what they have to say about it.Capriccioso
B
2

The simplest answer is some of them are falsey

var_dump(DateTime(false)); // date shown is current time

My bet is that the parser (which is trying to clean up a wide variety of acceptable inputs) strips the periods out (that are not being used as a delimiter), leaving only an empty string. It's the only explanation that makes sense.

echo strtotime('1.1.2000'); // outputs 946681200
Bighead answered 23/9, 2015 at 12:18 Comment(4)
This is probably the case. I would expect that strtotime(' ') and strtotime('') would give the same output though, but they don't. I'll file a bug and see if they can clarify this behaviour.Capriccioso
In which version echo strtotime(false); return the timestamp? i test here and got false. php5.6.8Lachellelaches
i have no willingness to go through this function lxr.php.net/xref/PHP_5_2/ext/date/php_date.c#1125 and php 5.1.0 pastebin.com/LYE0n7g3 may be you haveTimm
Ill mark tour answer as correct for now. I filed a bug report but no reaction as of yet.Capriccioso

© 2022 - 2024 — McMap. All rights reserved.