Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carbon Date Time Hours Comparison

How do I compare the hours between these two?

$today = Carbon::now(new \DateTimeZone('Asia/Jakarta'))->toDateTimeString();

and

$last = EmergencyOrder::select('CreatedDate')
  ->orderBy('CreatedDate', 'desc')
  ->first();
like image 370
Cookie Avatar asked Oct 03 '17 07:10

Cookie


1 Answers

From Carbon Docs

$today =  Carbon::now(new \DateTimeZone('Asia/Jakarta'));
$last = Carbon::parse(EmergencyOrder::select('CreatedDate')
                    ->orderBy('CreatedDate', 'desc')
                    ->first()->CreatedDate); //if there are no records it will fail

//check for equal
var_dump($today->eq($last));                     // bool(false)
//check for not equal
var_dump($today->ne($last));                     // bool(true)
//check $today < $last
var_dump($today->gt($last));                     // bool(false)
//check $today <= $last
var_dump($today->gte($last));                    // bool(false)
//check $today > $last
var_dump($today->lt($last));                     // bool(true)
//check $today >= $last
var_dump($today->lte($last));                    // bool(true)

And if you need the diference

$today->diffInHours($last);
$today->diffInMinutes($last);
$today->diffInDays($last);
like image 173
aaron0207 Avatar answered Oct 07 '22 06:10

aaron0207