2

Below is code to get the difference of used time in Laravel with Collective. but this code only counts in 24 hours nevertheless it over a day.

How could I modify my Collective code to achieve my goal? Thanks:)

This code made on Laravel 5.4v and Datatype of $time_used is DATETIME.

<div class="form-group">
    {!! Form::label('time_used', 'Time Used:') !!}
    <p>{!! \Carbon\Carbon::parse($job->deleted_at)->diff(\Carbon\Carbon::parse($job->created_at))->format('%H:%I:%S')!!}</p>
</div>
Mr. Pyramid
  • 3,855
  • 5
  • 32
  • 56
JsWizard
  • 1,663
  • 3
  • 21
  • 48

1 Answers1

1

For your purpose you are only displaying the Hours, Minutes and Seconds in the diff. You need to introduce a day indicator if it is more than 24 hours, example:

var_dump(\Carbon\Carbon::now()->diff(\Carbon\Carbon::parse('-2 day -3 hours -15 minutes'))->format('%ddays %H:%I:%S'));
Maraboc
  • 10,550
  • 3
  • 37
  • 48
Leon Vismer
  • 4,925
  • 1
  • 20
  • 22
  • thank you for you answer, but it didn't work. created_at and deleted_at datatype is Timestamp, perhaps that datatype must be change? – JsWizard Oct 02 '17 at 09:00
  • Ok then I presume `$job->deleted_at` and `$job->created_at` are default `timestamp` fields as per laravel default migration logic, so inside the database the field is a `timestamp`? – Leon Vismer Oct 02 '17 at 09:03
  • yes deleted_at and created_at's datatype is Timestamp and these are default values from Laravel. thx. – JsWizard Oct 02 '17 at 09:07
  • Perhaps you can then elaborate on *but it didn't work*? Does it throw an error or what does it display? If the item has not been deleted yet you will have an issue with what you are using, as the deleted_at timestamp would not have been set yet. – Leon Vismer Oct 02 '17 at 09:13
  • ok, it worked with just add %d. BTW how could i post this $time_used data to table in server by Collective? Thanks. – JsWizard Oct 02 '17 at 11:08
  • The **days** was just for display purposes, not sure what you mean by *Collective*, it would most probably be better to just add an accessor inside your model to bring back the time_difference. – Leon Vismer Oct 02 '17 at 11:33
  • LaravelCollective has nothing todo with your direct question. – Leon Vismer Oct 02 '17 at 11:41