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:
When the "Start" button is pressed, a date is added into the database table for that row.
When the "Stop" button is pressed, a date (end date)is added then I calculate the difference by:
$startTime = \Carbon\Carbon::parse($task->date_start); $finishTime = \Carbon\Carbon::parse($task->date_complete); $totalDuration = $finishTime->diffInSeconds($startTime);
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?
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: