PHP中的两次时间定义


Time Defference with Two Times in PHP

我有两个时间,如下所示:

$c _time = 11:32 AM
$t_time = 03:48 PM

现在我想找到00:00:00格式的差异

我该如何解决这个问题?

感谢

试试这个简单的解决方案:

$dt1 = new DateTime('11:32 AM');
$dt2 = new DateTime('03:48 PM');
echo $dt1->diff($dt2)->format('%H:%I:%S');

演示

试试这个:-

<?php
$datetime1 = new DateTime('11:32 AM');
$datetime2 = new DateTime('03:48 PM');
$interval = $datetime1->diff($datetime2);
echo "<pre/>";print_r($interval);
echo $interval->format('%H:%I:%S');
?>

输出:-

DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 0
    [h] => 4
    [i] => 16
    [s] => 0
    [invert] => 0
    [days] => 0
)
04:16:00

如果您使用Laravel,您可以使用Carbon进行一些简单的区分:

$c_time = "11:36 AM";
$t_time = "03:48 PM";
$c = Carbon::createFromFormat("H:i a", $c_time);
$t = Carbon::createFromFormat("H:i a", $t_time);
$minutes = $c->diffInMinutes($t); // returns 256 minutes, you can use either Carbon or pure DateTime objects to convert this