How to get datetime differance in laravel 4 -
i using laravel 4. facing problem finding difference between 2 date: 1 coming database table , 1 current datetime. date difference expecting 1 hour or 1 day. i've tried few solution can't fix yet. , don't know better way solve it. if guys have solution, please provide me example. please tell me if need library. here code:
$lecture_id = input::get('lecture_id'); $delegate_id = input::get('delegate_id'); // $newdate = new datetime(); $lecture = lecture::find($lecture_id); // $lec_date = date::forge($lecture->start_time); // $lec_date = new datetime($lecture->start_time); $lec_date = $lecture->start_time->diffforhumans(carbon::now()); if ( $lec_date > 1) { lecturedelegate::create(array( 'lecture_id' => input::get('lecture_id'), 'delegate_id'=> input::get('delegate_id') )); return redirect::to('/')->with('message', 'your apply lecture'); }
should be:
$lec_date = carbon::createfromtimestamp( strtotime( $lecture->start_time ) )->diffforhumans();
or possibly:
$lec_date = $lecture->start_time->diffforhumans();
if add lecture.php
model:
public function getdates() { return array('created_at', 'updated_at', 'deleted_at', 'start_time'); }
from documentation:
by default, eloquent convert created_at, updated_at, , deleted_at columns instances of carbon...
you may customize fields automatically mutated, , disable mutation, overriding getdates method of model.
as diffforhumans
documentation states:
the lone argument function other carbon instance diff against, , of course defaults now() if not specified.
update
if timestamp database being passed diffforhumans
in future, carbon automatically makes return like:
when comparing value in future default now:
- 1 hour
- 5 months now
when comparing value in past value:
- 1 hour before
- 5 months before
Comments
Post a Comment