Laravel / Carbon 将两个时间戳加在一起?

Laravel / Carbon adding two timestamps together?

我正在开发一个系统,人们可以在该系统上启动和停止计算所花费时间的东西。这是由以下人员完成的:

并且 "timeSpent" $totalDuration 被存储为时间戳。

要求已更改,因此用户可以 stop/start 但它仍然保留总时间。我的想法是:

使用与下面相同的过程,始终存储 timeSpent,因此如果用户再次单击 start/stop,我可以像之前那样计算差异,然后将其添加到 timeSpent 存储在数据库中,但是我似乎无法弄清楚如何执行此操作。我试过:

$totalTimeSpent = strtotime($task->time_spent) +
strtotime($totalDuration);

然后再解析:

$total = \Carbon\Carbon::parse($totalTimeSpent);

但这会导致错误:

Failed to parse time string (1463184003) at position 7 (0): Unexpected character

有人可以提出替代方案吗?

您不应将 $totalDuration 存储在类型为 timestamp 的字段中,因为它不是时间戳。它只是秒数,应该存储在整数字段中。

一旦为此使用整数类型,添加数字就不会有任何问题:

$total = $task->time_spent + $totalDuration;