How to find time difference between two dates using PHP

前端 未结 4 1171
失恋的感觉
失恋的感觉 2021-01-06 10:20

How to find time difference between two dates using PHP.

For example i am having two dates:

start Date : 2010-07-30 00:00:00

4条回答
  •  天涯浪人
    2021-01-06 10:39

    But i need like following : 24hrs 3 minutes 5 seconds

    If you're using PHP 5.3 or better (which you should be), you can use the built in DateTime class to produce a DateInterval which can be formatted easily.

    $time_one = new DateTime('2010-07-29 12:43:54');
    $time_two = new DateTime('2010-07-30 01:23:45');
    $difference = $time_one->diff($time_two);
    echo $difference->format('%h hours %i minutes %s seconds');
    

    DateTime was introduced in 5.1, but DateInterval is new to 5.3.

提交回复
热议问题