How to calculate total time in laravel

  laravel, php, time

I have Project about timesheet, i have tasks table and timers table, each task has many timers and timers belong to task.

I calculate the time between start_at and end_at in getTotalAttribute as you see in the pictures down, and i got the difference between them in minutes.

Timer Model

class TsTimer extends Model
    use HasFactory;

    public $appends = ['total'];

    public $casts = [
        'start' => 'datetime',
        'end' => 'datetime',

    public function task(){
        return $this->belongsTo(TsTask::class);

    public function user(){
        return $this->belongsTo(User::class);

    public function getTotalAttribute(){
        // calclute the difference between two time start and end in mints.
            return $this->start->diffInMinutes($this->end);
    return 0;

Task Model

class TsTask extends Model
    use HasFactory;

    public $appends = ['total', 'timer_total'];

    public function project(){
        return $this->belongsTo(TsProject::class);

    public function timers(){
        return $this->hasMany(TsTimer::class,'task_id');

    public function getTotalAttribute(){
        return date('i:s',$format);

And now i need to calculate total time for these timers, like for example first timer (4hours – 10 min), second timer (1 hour), the total will be 5:10.

So i need to calculate this total , any help or suggestion please, i have no idea how to calculate it.

Thanks in advance

Here photo about app:

App overview Image

Source: Laravel

Leave a Reply