'I'm working on total time spent functionality

I have added start, stop pause buttons and storing time spans in db. how can i calculate timespan based on id in Laravel. started time -2021-11-16 05:37:40 paused - 2021-11-16 05:48:40 resumed - 2021-11-16 05:52:07 stoped -2021-11-16 05:58:07

need to calculate time spans between started paused and resumed stoped and add both in Laravel 8.



Solution 1:[1]

use Carbon in Laravel, import it and check different methods

use Illuminate\Support\Carbon;

$start = Carbon::parse('2021-11-16 05:37:40');
$paused = Carbon::parse('2021-11-16 05:48:40');
        

$difference = $paused->diffInMinutes($start);
$difference = $paused->diffInDays($start);
$difference = $paused->diffInHours($start);
$difference = $paused->diffInMicroseconds($start);
$difference = $paused->diffInMinutes($start);
$difference = $paused->diffInMonths($start);
$difference = $paused->diffInQuarters($start);
$difference = $paused->diffInSeconds($start);
$difference = $paused->diffInWeekdays($start);
$difference = $paused->diffInWeekendDays($start);
$difference = $paused->diffInWeeks($start);
$difference = $paused->diffInYears($start);
$difference = $paused->diffAsCarbonInterval($start);
$difference = $paused->diffForHumans($start);

echo $difference;
        
$resumed = Carbon::parse('2021-11-16 05:52:07');
$stopped = Carbon::parse('2021-11-16 05:58:07');

//do the same with $resumed and $stopped like in example I did it with $start and $paused

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shailendra