Convert DateInterval object to seconds in php

后端 未结 3 936
时光取名叫无心
时光取名叫无心 2021-02-05 08:23
$datetime1 = date_create(\'2009-10-11\');
$datetime2 = date_create(\'2009-10-13\');
$interval = date_diff($datetime1, $datetime2);

How do i convert the

3条回答
  •  一向
    一向 (楼主)
    2021-02-05 09:05

    There is a function format for this. But it wont return the number of seconds. To get number of seconds you use this technique

    $seconds = abs($datetime1->getTimestamp()-$datetime2->getTimestamp());
    

    If you really want to use $interval you need to calculate it.

    $seconds = $interval->days*86400 + $interval->h*3600 
               + $interval->i*60 + $interval->s;
    

    Here

    • 86400 is the number of seconds in a day
    • 3600 is the number of seconds in an hour
    • 60 is the number of seconds in a minute

提交回复
热议问题