Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does strtotime('x') return tomorrows date

Tags:

php

strtotime

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.

like image 838
Laurence Avatar asked Sep 03 '12 12:09

Laurence


2 Answers

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, strtotime returns a timestamp and date prints 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:

$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);
}

This prints

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 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, strtotime returns false and this has the same effect as if you had called date("d-m-Y", 0) -- it formats the start of the epoch (January 1st 1970, 00:00:00). This happens because false is converted to the integer 0 as 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).

like image 88
Jon Avatar answered Oct 23 '22 04:10

Jon


  • 214454 is treated as hour:minute:second
  • 333333 it's because there is no 33 hour
like image 43
Peter Avatar answered Oct 23 '22 05:10

Peter