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?

JurrieG
  • 93
  • 4
  • You're passing in strings....what would you expect it to return? – Jay Blanchard Sep 23 '15 at 12:02
  • 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 output – Mark Baker Sep 23 '15 at 12:03
  • Hey, it works with commas too! – Amarnasan Sep 23 '15 at 12:04
  • 4
    Interesting question: _Why are you doing that_? – RiggsFolly Sep 23 '15 at 12:04
  • 1
    It should return 'false' on something it can't parse it as a date/time according to the documentation. – JurrieG Sep 23 '15 at 12:05
  • 2
    Then [raise a bug](https://bugs.php.net/) if you think that this is incorrect behaviour – Mark Baker Sep 23 '15 at 12:06
  • it takes default date and time and return a timestamp –  Sep 23 '15 at 12:08
  • Seriously, what the hell is this: ',. ., ,. .,1 week ,. ., 2 week,. .,3 week' – Amarnasan Sep 23 '15 at 12:11
  • 1
    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/time – JurrieG Sep 23 '15 at 12:13
  • 1
    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 speculation – Mark Baker Sep 23 '15 at 12:15
  • Thanks. I'll file a bug and see what they have to say about it. – JurrieG Sep 23 '15 at 12:22

1 Answers1

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
Machavity
  • 30,841
  • 27
  • 92
  • 100
  • 1
    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. – JurrieG Sep 23 '15 at 12:21
  • In which version `echo strtotime(false);` return the timestamp? i test here and got false. php5.6.8 – rray Sep 23 '15 at 12:28
  • i have no willingness to go through this function http://lxr.php.net/xref/PHP_5_2/ext/date/php_date.c#1125 and php 5.1.0 http://pastebin.com/LYE0n7g3 may be you have – NullPoiиteя Sep 23 '15 at 12:51
  • Ill mark tour answer as correct for now. I filed a bug report but no reaction as of yet. – JurrieG Sep 24 '15 at 15:45