Carbon Date Time Hours Comparison
Asked Answered
B

4

6

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();
Beanstalk answered 3/10, 2017 at 7:44 Comment(0)
P
9

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 difference

$today->diffInHours($last);
$today->diffInMinutes($last);
$today->diffInDays($last);
Piquant answered 3/10, 2017 at 7:51 Comment(2)
ho thanks for the reply, btw this doesnt work, here's the error: (1/1) FatalThrowableError Call to a member function diffInHours() on stringBeanstalk
I miss to init the $today and $last variables, check itPiquant
C
0

better version of this answer

$today =  Carbon::now(new \DateTimeZone('Asia/Jakarta'));
$last = EmergencyOrder::select('CreatedDate')
                    ->orderBy('CreatedDate', 'desc')
                    ->first();
if($last) {
$createdDate = Carbon::parse($last->createdDate);
$today->diffInHours($createdDate);
$today->diffInMinutes($createdDate);
$today->diffInDays($createdDate);
}

this will not gonna give any error if it fails.

Costin answered 3/10, 2017 at 7:59 Comment(0)
H
0

// All have short hand aliases and PHP equivalent code:

var_dump($first->eq($second));                     // bool(false)
var_dump($first->equalTo($second));                // bool(false)
var_dump($first == $second);                       // bool(false)

var_dump($first->ne($second));                     // bool(true)
var_dump($first->notEqualTo($second));             // bool(true)
var_dump($first != $second);                       // bool(true)

var_dump($first->gt($second));                     // bool(false)
var_dump($first->greaterThan($second));            // bool(false)
var_dump($first->isAfter($second));                // bool(false)
var_dump($first > $second);                        // bool(false)

var_dump($first->gte($second));                    // bool(false)
var_dump($first->greaterThanOrEqualTo($second));   // bool(false)
var_dump($first >= $second);                       // bool(false)

var_dump($first->lt($second));                     // bool(true)
var_dump($first->lessThan($second));               // bool(true)
var_dump($first->isBefore($second));               // bool(true)
var_dump($first < $second);                        // bool(true)

var_dump($first->lte($second));                    // bool(true)
var_dump($first->lessThanOrEqualTo($second));      // bool(true)
var_dump($first <= $second);                       // bool(true)
Hardandfast answered 17/8, 2020 at 7:36 Comment(0)
H
0
echo Carbon::now()->tzName;                        // UTC
$first = Carbon::create(2012, 9, 5, 23, 26, 11);
$second = Carbon::create(2012, 9, 5, 20, 26, 11, 'America/Vancouver');

echo $first->toDateTimeString();                   // 2012-09-05 23:26:11
echo $first->tzName;                               // UTC
echo $second->toDateTimeString();                  // 2012-09-05 20:26:11
echo $second->tzName;                              // America/Vancouver

var_dump($first->equalTo($second));                // bool(false)
// equalTo is also available on CarbonInterval and CarbonPeriod
var_dump($first->notEqualTo($second));             // bool(true)
// notEqualTo is also available on CarbonInterval and CarbonPeriod
var_dump($first->greaterThan($second));            // bool(false)
// greaterThan is also available on CarbonInterval
var_dump($first->greaterThanOrEqualTo($second));   // bool(false)
// greaterThanOrEqualTo is also available on CarbonInterval
var_dump($first->lessThan($second));               // bool(true)
// lessThan is also available on CarbonInterval
var_dump($first->lessThanOrEqualTo($second));      // bool(true)
// lessThanOrEqualTo is also available on CarbonInterval

$first->setDateTime(2012, 1, 1, 0, 0, 0);
$second->setDateTime(2012, 1, 1, 0, 0, 0);         // Remember tz is 'America/Vancouver'

var_dump($first->equalTo($second));                // bool(false)
var_dump($first->notEqualTo($second));             // bool(true)
var_dump($first->greaterThan($second));            // bool(false)
var_dump($first->greaterThanOrEqualTo($second));   // bool(false)
var_dump($first->lessThan($second));               // bool(true)
var_dump($first->lessThanOrEqualTo($second));      // bool(true)
Hardandfast answered 17/8, 2020 at 7:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.