Convert DateInterval object to seconds in php

后端 未结 3 1053
时光取名叫无心
时光取名叫无心 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:20

    Another way to get the number of seconds in an interval is to add it to the zero date, and get the timestamp of that date:

    $seconds = date_create('@0')->add($interval)->getTimestamp();
    

    This method will handle intervals created via the DateInterval contructor more or less correctly, whereas shiplu's answer will ignore years, months and days for such intervals. However, shiplu's answer is more accurate for intervals that were created by subtracting two dates. For intervals consisting only of hours, minutes and seconds, both methods will get the correct answer.

提交回复
热议问题