Why strtotime ('x') returns a date tomorrow

I can't figure it out - why the following happens:

echo date("dmY", 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 simply 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 that I did to try to figure out how / why some values ​​were returned from a specific function.

This causes my function to fail - because you must accept "a" (or any single char), which will be returned as an invalid invalid date.

+8
php strtotime
source share
2 answers

Both single and double characters are interpreted as timezones (table "used characters", string "tz"); six digits are interpreted as HHMMII (table "24-hour notation", line "Hour, minutes and seconds, without a colon").

Valid Formats

In the first and third cases, parsing succeeds, strtotime returns a timestamp, and date prints any date that it matches. Obviously, why the first case succeeds; for the 3rd case, remember that military time zones can be assigned to one letter, and then the results make sense. Let this code follow:

 $zones = range('a', 'z'); $last = null; foreach($zones as $tz) { $ts = strtotime($tz); if ($last !== null) { echo "TZ $last[0] => TZ $tz: change = ".($ts - $last[1])."\n"; } $last = array($tz, $ts); } 

Will print

 TZ a => TZ b: change = -3600 TZ b => TZ c: change = -3600 TZ c => TZ d: change = -3600 TZ d => TZ e: change = -3600 TZ e => TZ f: change = -3600 TZ f => TZ g: change = -3600 TZ g => TZ h: change = -3600 TZ h => TZ i: change = -3600 TZ i => TZ j: change = -1346649725 TZ j => TZ k: change = 1346646125 TZ k => TZ l: change = -3600 TZ l => TZ m: change = -3600 TZ m => TZ n: change = 46800 TZ n => TZ o: change = 3600 TZ o => TZ p: change = 3600 TZ p => TZ q: change = 3600 TZ q => TZ r: change = 3600 TZ r => TZ s: change = 3600 TZ s => TZ t: change = 3600 TZ t => TZ u: change = 3600 TZ u => TZ v: change = 3600 TZ v => TZ w: change = 3600 TZ w => TZ x: change = 3600 TZ x => TZ y: change = 3600 TZ y => TZ z: change = -43200 

You can see continuity as we move from one time zone to another; a gap in the time zone 'j' that does not exist (and parsing is not performed with the same results as described below); the gap in the time zone is 'n' , where we go from UTC + 12 to UTC-1, etc.

Invalid formats

In the 2nd and 4th cases, parsing fails, strtotime returns false , and this has the same effect as you called date("dmY", 0) - it formats the beginning of the era (January 1, 1970, 00: 00: 00). This is because false converted to an integer 0 in accordance with the normal type of juggling rules .

What does strtotime('x') really return?

It returns the current time in the UTC-11 time zone (this is the β€œX-ray” time zone). Depending on your local time and time zone, this is usually either "today" or "tomorrow" from your point of view. If you are less than UTC-11, then it may even be "yesterday" (although that is not very likely ).

+7
source share
  • 214454 considered hour: minute: second
  • 333333 this is because there is no 33 hours
+3
source share

All Articles