trzew
trzew

Reputation: 455

Calculating the time difference in Laravel

I am beginner webdeveloper.

I make my website in Laravel 5.8.

This is my Model and migration:

class Timecycle extends Model
{

    protected $fillable = [
        'case_id',
        'timecycleable_id',
        'timecycleable_type',
        'status',
        'worked_time'
    ];

    protected $casts = [
        'id' => 'int',
        'case_id' => 'int',
    ];

    protected $dates = [
        'created_at',
        'updated_at'
    ];

    public function stopwatch()
    {
        return $this->morphTo();
    }


Schema::create('timecycles', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('timecycleable_id');
            $table->string('timecycleable_type');
            $table->integer('case_id')->unsigned();
            $table->foreign('case_id')->references('id')->on('case_instances')->onDelete('cascade');
            $table->boolean('status')->default(0);
            $table->integer('worked_time')->default(0);
            $table->timestamps();
        });

Controller:

public function sync()
{
    $activeStopwatches = $this->getActiveStopwatches();
    foreach ($activeStopwatches as $activeStopwatch) {
        foreach ($activeStopwatch->timeCycle as $timeCycle) {
            $newTime = now() - $timeCycle->updated_at;
            $timeCycle->update(['worked_time' => $timeCycle->worked_time + $newTime]);
        }
    }
}

public function getActiveStopwatches()
    {
        return $this->stopwatch::with(['timeCycle' => function ($q) {
            $q->where('status', 1);
        }, 'caseInstance'])
            ->where('user_id', Auth()->user()->id)
            ->whereHas('timeCycle', function ($q) {
                $q->where('status', 1);
            })
            ->get();
    }

I have a method that displays the current stopwatches: getActiveStopwatches. Works correctly. In the sync () function, I'd like to update the worked_time. So: current time - last update date, save the result to worked_time.

Is this entry for calculations correct? The time I want to save (the $ newTime variable) is to be expressed in seconds.

Upvotes: 0

Views: 2406

Answers (2)

B. Boisson
B. Boisson

Reputation: 16

You cannot substract 2 dates like this, with Carbon you can use the ->diff() method. This will return a DateInterval object.

To get the difference in seconds you can use ->diffInSeconds() method.

$newTime = now()->diffInSeconds($timeCycle->updated_at);

Or substract timestamps :

now()->getTimestamp() - $timeCycle->updated_at->getTimestamp()

Upvotes: 0

Qamar Rafhan
Qamar Rafhan

Reputation: 129

@trzew I hope blow code will help you.

use Carbon\Carbon;


 $newTime = now() - $timeCycle->updated_at; 
is not working fine , use time difference function to calculate difference   

$mytime = Carbon\Carbon::now();
$totalDuration = $newTime->diffInSeconds($timeCycle->updated_at);
 gmdate('H:i:s', $totalDuration);
// 00:00:21

Upvotes: 2

Related Questions