如何在 PHP 中计算两个时间(不是日期时间)之间的秒数差异。Laravel // PHP // Carbon

lar*_*arb 1 php time laravel difference

下面举一个例子来说明这个问题。下面的两个例子都给出了错误的区别:

// gives 86398 while the correct is 2sec
$diff_in_sec = strtotime('23:59:59') - strtotime('00:00:01'); 

// again gives 86398 while the correct is 2sec.
$diff_in_sec = Carbon::parse('00:00:01')->diffInSeconds(Carbon::parse('23:59:59')); 
Run Code Online (Sandbox Code Playgroud)

我想要的是23:59:59与 相比00:00:01返回 2 秒的差异,并 00:00:0123:59:59.

小智 5

不幸的是,您需要将它们转换为日期时间。php 如何知道 23:59:59 和 00:00:01 不是在同一天发生?