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

前端 未结 5 911
星月不相逢
星月不相逢 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:30

    Because strtotime() outputs the number of seconds relative to the second argument (in your case, the Unix epoch (December 31, 1969 19:00:00)).

    The negative numbers is expected because "3:15:00" is 56700 seconds before the Unix epoch.

    0 讨论(0)
  • 2020-12-12 01:34

    If you want to do something like that, I think you want to just do some math on the time strings themselves and convert them to a number of seconds, like this:

    <?php
    function hmstotime($hms)
    {
        list($hours, $minutes, $seconds) = explode(":",$hms);
        return $hours * 60 * 60 + $minutes * 60 + $seconds;
    }
    ?>
    
    0 讨论(0)
  • 2020-12-12 01:36

    Try it without the second parameter. That's supposed to be a timestamp for the returned time to be relative to. Giving it 0 means you're asking for a timestamp relative to the Unix epoch.

    In response to your comment:

    It's not documented functionality, but I use strtotime("HH:MM") all the time, and it returns a timestamp relative to the current time. I guess if you want to be sure though, you could do this:

    strtotime("3:15:00",time());
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-12 01:43

    strtotime() without a second argument gets the time from the supplied string and fills in the blanks from the current date:

    echo date('Y-m-d H:i:s', strtotime("3:15:00"));
    -> 2009-06-30 03:15:00
    

    With a second argument it calculates the date relative to the second argument:

    echo date('Y-m-d H:i:s', strtotime("3:15:00", 0));
    -> 1970-01-01 03:15:00
    

    To calculate the difference between two timestamps in seconds, you can just do this:

    echo strtotime("3:15:00") - strtotime("3:00:00");
    -> 900
    

    Edit: Of course taking into account which is the bigger number:

    $t1 = strtotime("3:15:00");
    $t2 = strtotime("3:30:00");
    
    $diff = max($t1, $t2) - min($t1, $t2);
    $diff = abs($t1 - $t2);
    

    Or something of that nature...

    0 讨论(0)
提交回复
热议问题