How to get millisecond between two dateTime obj?

前端 未结 5 457
南方客
南方客 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: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.

    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 "
    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

提交回复
热议问题