如何获得两次已经使用此格式的('h:m:s')之间的时间差

时间:2019-09-22 00:40:26

标签: laravel laravel-5 time php-carbon

我想以hour:min:sec格式获得两次区别。例如,开始时间02:00:00和结束时间02:30:00的差应为00:30:00

我尝试使用Carbon,但我认为它支持不同的格式

<td> {{ \Carbon\Carbon::now()->diff(\Carbon\Carbon::parse($sales->created_at))}}</td>

我想在hour:min:sec输入中也有所不同

2 个答案:

答案 0 :(得分:0)

您的代码运行正常,

$diff = \Carbon\Carbon::now()->diff(\Carbon\Carbon::parse('12:50:35'));

这将返回DateInterval对象,

DateInterval {#242 ▼
  interval: + 11:18:14.006875
  +"y": 0
  +"m": 0
  +"d": 0
  +"h": 11
  +"i": 18
  +"s": 14
  +"f": 0.006875
  +"weekday": 0
  +"weekday_behavior": 0
  +"first_last_day_of": 0
  +"invert": 0
  +"days": 0
  +"special_type": 0
  +"special_amount": 0
  +"have_weekday_relative": 0
  +"have_special_relative": 0
}

然后您可以获取小时,秒,分钟,天等的差异。

$diff->h, $diff->i, $diff->s, $diff->days

您的输入应该是

$diff->h .':'. $diff->i .':'. $diff->s

否则,您可以使用

$diffInhours = \Carbon\Carbon::now()->diffInHours(\Carbon\Carbon::parse('12:50:35');
$diffInMinutes = \Carbon\Carbon::now()->diffInMinutes(\Carbon\Carbon::parse('12:50:35'));

many other ways

答案 1 :(得分:0)

::after函数返回 DateInterval 对象。

可以通过调用import logo from './logo.png'; // this will tell webpack that you are using this file, therefore it will be included like logo.<chunk>.png 函数从DateInterval对象中检索您的预期结果。

diff()