Phorce
Phorce

Reputation: 4642

Laravel / Carbon adding two timestamps together?

I am working on a system where people can start and stop something which calculates the time it has taken them. This is done by:

And also "timeSpent" $totalDuration is stored as a timestamp.

The requirements have changed so that the user can stop/start but it still holds the the total time. My idea is:

With the same process as below, the timeSpent is always stored, so if a user clicks start/stop again I can just calculate the difference as done previously and then add it to the timeSpent that is stored inside the database, however I can't seem to figure out how to do this. I have tried:

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

Then parse it again:

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

But this throughs up an error:

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

Can someone suggest an alternative?

Upvotes: 1

Views: 2409

Answers (1)

jedrzej.kurylo
jedrzej.kurylo

Reputation: 40909

You shouldn't store $totalDuration in a field with type timestamp because it's not a timestamp. It's just a number of seconds and should be stored in an integer field.

Once you use integer type for that, you shouldn't have any issues with adding the numbers:

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

Upvotes: 4

Related Questions