Calculate number of hours between 2 dates in PHP

前端 未结 16 1104
轮回少年
轮回少年 2020-11-22 14:07

How do I calculate the difference between two dates in hours?

For example:

day1=2006-04-12 12:30:00
day2=2006-04-14 11:30:00

In thi

16条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 14:46

    This is working in my project. I think, This will be helpful for you.

    If Date is in past then invert will 1.
    If Date is in future then invert will 0.

    $defaultDate = date('Y-m-d');   
    $datetime1   = new DateTime('2013-03-10');  
    $datetime2   = new DateTime($defaultDate);  
    $interval    = $datetime1->diff($datetime2);  
    $days        = $interval->format('%a');
    $invert      = $interval->invert;
    

提交回复
热议问题