How to get millisecond between two dateTime obj?

前端 未结 5 455
南方客
南方客 2020-12-11 18:06

How to get millisecond between two DateTime objects?

$date = new DateTime();
$date2 = new DateTime(\"1990-08-07 08:44\");

I t

相关标签:
5条回答
  • 2020-12-11 18:41

    Sorry to digg out an old question, but I've found a way to get the milliseconds timestamp out of a DateTime object:

    function dateTimeToMilliseconds(\DateTime $dateTime)
    {
        $secs = $dateTime->getTimestamp(); // Gets the seconds
        $millisecs = $secs*1000; // Converted to milliseconds
        $millisecs += $dateTime->format("u")/1000; // Microseconds converted to seconds
        return $millisecs;
    }
    

    It requires however that your DateTime object contains the microseconds (u in the format):

    $date_str = "20:46:00.588";
    
    $date = DateTime::createFromFormat("H:i:s.u", $date_str);
    

    This is working only since PHP 5.2 hence the microseconds support to DateTime has been added then.

    With this function, your code would become the following :

    $date_str = "1990-08-07 20:46:00.588";
    $date1 = DateTime::createFromFormat("Y-m-d H:i:s.u", $date_str);
    
    $msNow = (int)microtime(true)*1000;
    
    echo $msNow - dateTimeToMilliseconds($date1);
    
    0 讨论(0)
  • 2020-12-11 18:44

    In the strict sense, you can't.

    It's because the smallest unit of time for the DateTime class is a second.

    If you need a measurement containing milliseconds then use microtime()


    Edit:

    On the other hand if you simply want to get the interval in milliseconds between two ISO-8601 datetimes then one possible solution would be

    function millisecsBetween($dateOne, $dateTwo, $abs = true) {
        $func = $abs ? 'abs' : 'intval';
        return $func(strtotime($dateOne) - strtotime($dateTwo)) * 1000;
    }
    

    Beware that by default the above function returns absolute difference. If you want to know whether the first date is earlier or not then set the third argument to false.

    // Outputs 60000
    echo millisecsBetween("2010-10-26 20:30", "2010-10-26 20:31");
    
    // Outputs -60000 indicating that the first argument is an earlier date
    echo millisecsBetween("2010-10-26 20:30", "2010-10-26 20:31", false);
    

    On systems where the size of time datatype is 32 bits, such as Windows7 or earlier, millisecsBetween is only good for dates between 1970-01-01 00:00:00 and 2038-01-19 03:14:07 (see Year 2038 problem).

    0 讨论(0)
  • 2020-12-11 18:51

    DateTime dates are only stored as whole seconds. If you still need the number of milliseconds between two DateTime dates, then you can use getTimestamp() to get each time in seconds (then get the difference and turn it into milliseconds):

    $seconds_diff = $date2.getTimestamp() - $date.getTimestamp()
    $milliseconds_diff = $seconds_diff * 1000
    
    0 讨论(0)
  • 2020-12-11 18:55

    DateTime supports microseconds since 5.2.2. This is mentioned in the documentation for the date function, but bears repeating here. You can create a DateTime with fractional seconds and retrieve that value using the 'u' format string.

    <?php
    // Instantiate a DateTime with microseconds.
    $d = new DateTime('2011-01-01T15:03:01.012345Z');
    
    // Output the microseconds.
    echo $d->format('u'); // 012345
    
    // Output the date with microseconds.
    echo $d->format('Y-m-d\TH:i:s.u'); // 2011-01-01T15:03:01.012345
    
    // Unix Format
    echo "<br>d2: ". $d->format('U.u');
    
    function get_data_unix_ms($data){
        $d = new DateTime($data);
        $new_data = $d->format('U.u');
        return $new_data;
    }
    
    function get_date_diff_ms($date1, $date2)
        {
            $d1 = new DateTime($date1);
            $new_d1 = $d1->format('U.u');
    
            $d2 = new DateTime($date2);
            $new_d2 = $d2->format('U.u');
    
            $diff = abs($new_d1 - $new_d2);
    
            return $diff;
        }
    

    https://www.php.net/manual/en/class.datetime.php

    0 讨论(0)
  • 2020-12-11 19:04

    Here's a function to do that + tests.

    https://gist.github.com/vudaltsov/0bb623b9e2817d6ce359eb88cfbf229d

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