Strange behavior of PHP time math: Why is strtotime() returning negative numbers?

前端 未结 5 913
星月不相逢
星月不相逢 2020-12-12 00:51

I\'m trying to do some very basic time math - basically, given inputs of time and distance, calculate the speed. I chose to use strtotime() to convert the time inputs into s

5条回答
  •  天涯浪人
    2020-12-12 01:37

    Apparently with just bare times PHP is assigning the date December 31, 1969. When I ran this:

    echo date('F j, Y H:i:s', $t1) . "\n";
    echo date('F j, Y H:i:s', $t2) . "\n";
    echo date('F j, Y H:i:s', $t3) . "\n";
    echo date('F j, Y H:i:s', $t4) . "\n";
    

    I got this:

    December 31, 1969 03:15:00
    December 31, 1969 01:00:00
    December 31, 1969 02:00:00
    December 31, 1969 09:00:00
    

    Remember that strtotime returns a UNIX timestamp, which is defined as the number of seconds since January 1, 1970. By definition a UNIX timestamp refers to a specific month/day/year, so despite the name strtotime is not really intended for bare times without dates.

提交回复
热议问题