I cant understand this – why does the following occur?
echo date("d-m-Y", strtotime($str));
$str = '214454'; // Produces todays date
$str = '333333'; // Produces 1-1-1970
$str = 'a' (or ANY single char) // Produces tomorrows date
$str = 'aa' (or ANY double char) // Produces 1-1-1970
OR just returning the strtotime function
echo strtotime($str);
$str = '214454'; // Produces todays date
$str = '333333'; // returns false
$str = 'a' (or ANY single char) // Produces tomorrows date
$str = 'aa' (or ANY double char) // returns false
These values came from some testing I was doing, to try and work out how/why certain values were being returned from a specific function.
Its causing my function to fail – because you would assume “a” (or any single char) to be returned as a false incorrect date.
Both single and double characters are interpreted as timezones (table “used symbols”, row “tz”); six digits are interpreted as
HHMMII(table “24 Hour Notation”, row “Hour, minutes and seconds, no colon”).Valid formats
In the 1st and 3rd cases parsing succeeds,
strtotimereturns a timestamp anddateprints whatever date it corresponds to. It’s obvious why the 1st case succeeds; for the 3rd case, remember that military time zones can be referred to with a single letter, and then the results make sense. Let’s follow this code:This prints
You can see the continuity as we go from one time zone to the next; the discontinuity for timezone
'j'which does not exist (here parsing fails with the same results explained below); the discontinuity at timezone'n'where we move from UTC+12 to UTC-1, etc.Invalid formats
In the 2nd and 4th cases parsing fails,
strtotimereturnsfalseand this has the same effect as if you had calleddate("d-m-Y", 0)— it formats the start of the epoch (January 1st 1970, 00:00:00). This happens becausefalseis converted to the integer0as per normal type juggling rules.What does
strtotime('x')really return?It returns the current time at the timezone UTC-11 (this is the “x-ray” military time zone). Depending on your local time and timezone, this is usually either “today” or “tomorrow” from your point of view. If you happen to be at less than UTC-11 then it could even be “yesterday” (although that’s not quite likely).