0

I wish to calculate $time_used with using Collective and Blade in Laravel.

All date format of time_start, time_stop and time_used is timestamp in DB.

I tried to calculate with using Carbon, but I couldn't got time_used.

Also "time_start" from "created_at" and "time_stop" from "deleted_at" in DB.

Could you tell me how to get the time difference. Thanks.

in \show_fields.blade.php

    <!-- Time Start Field -->
    <div class="form-group">
        {!! Form::label('time_start', 'Time Started:') !!}
        <p>{!! $job->created_at !!}</p>
    </div>

    <!-- Time Stop Field -->
    <div class="form-group">
        {!! Form::label('time_stop', 'Time Stopped:') !!}
          <p>{!! $job->deleted_at !!}</p>            
    </div>

    <!-- Time Used Field -->
    <div class="form-group">
        {!! Form::label('time_used', 'Time Used:') !!}
        <p>{!! $job->time_used !!}</p>
    </div>

Result was as below...

Job Started:
2017-09-17 17:51:39

Job Stopped:
2017-09-17 17:51:49

Time Used:
JsWizard
  • 1,663
  • 3
  • 21
  • 48

1 Answers1

0

You can get the difference in seconds between the two timestamps and then format it to H:i:s

gmdate("H:i:s",Carbon::parse($job->deleted_at)->diffInSeconds(Carbon::parse($job->created_at)));

Or

Carbon::parse($job->deleted_at)->diff(Carbon::parse($job->created_at))->format('%H:%I:%S');

Which should return something like 00:00:10 in the format Hours:Minutes:Seconds

Norris Oduro
  • 1,019
  • 8
  • 17
  • Thank you @Norris , the second one's result was [ErrorException] Call to a member function diff() on string. have you other way? – JsWizard Sep 17 '17 at 19:21
  • {!! gmdate("H:i:s",$job->deleted_at->diffInSeconds($job->created_at))!!} is it right? – JsWizard Sep 17 '17 at 19:26
  • Error was Call to a member function diffInSeconds() on string – JsWizard Sep 17 '17 at 19:31
  • this means the dates are strings and not carbon instances. I edited my answer and parsed the dates to carbon instances – Norris Oduro Sep 17 '17 at 19:39
  • Thank you so much @Norris... the second one was working...:) as {!! \Carbon\Carbon::parse($job->deleted_at)->diff(\Carbon\Carbon::parse($job->created_at))->format('%H:%I:%S')!!} – JsWizard Sep 17 '17 at 19:44
  • And.... @Norris, How could I put the $time_used value to server when I deleted colum in table? thanks – JsWizard Sep 17 '17 at 19:46
  • Do you have a time_used field in your database or you want to perform an action based on the value on the server – Norris Oduro Sep 17 '17 at 19:48
  • Yes. I have time_used field. also time_stop, time_start, created_at, updated_at, deleted_at, too. :) – JsWizard Sep 17 '17 at 19:53
  • `public function destroy($id) { $job = $this->jobRepository->findWithoutFail($id); if (empty($job)) { Flash::error('Job not found'); return redirect(route('jobs.index')); } $job->time_used = \Carbon\Carbon::parse($job->deleted_at)->diff(\Carbon\Carbon::parse($job->created_at))->format('%H:%I:%S'); $this->jobRepository->delete($id); Flash::success('Job stop successfully.'); return redirect(route('jobs.index')); } ` Is it correct? @Norris – JsWizard Sep 17 '17 at 20:00
  • Are you using laravel 5.5? – Norris Oduro Sep 17 '17 at 20:01
  • Do $Job->save(); after setting the time used – Norris Oduro Sep 17 '17 at 20:04
  • You might also want to clean your code by moving this into a method in your repository – Norris Oduro Sep 17 '17 at 20:06
  • The result was "0000-00-00 00:00:00".... Could you tell me more detailed code? I made `$job->time_used = \Carbon\Carbon::parse($job->deleted_at)->diff(\Carbon\Carbon::parse($job->created_at))->format('%H:%I:%S'); $job->save(); $this->jobRepository->delete($id); Flash::success('Job stop successfully.'); return redirect(route('jobs.index')); }` Thanks.@Norris – JsWizard Sep 17 '17 at 20:11
  • @Magnetic change your migration column type of the `$time_used` from `dateTime` to `time` and do `$job->time_used = \Carbon\Carbon::parse($job->deleted_at)->diff(\Carbon\Carbon‌​::parse($job->create‌​d_at))->format('%H:%‌​I:%S'); $job->save();` – Norris Oduro Sep 17 '17 at 21:31
  • thank you for your friendly answer :) the result was `Class 'Carbon‌​‌​\Carbon‌​‌​' not found` so I did defined `use Carbon\Carbon;` but it was same result...what is problem to use Carbon? – JsWizard Sep 18 '17 at 07:47
  • But it was working before? Hope you didn't add the code to the migration rather – Norris Oduro Sep 18 '17 at 07:50