碳日期时间小时比较

时间:2017-10-03 07:44:02

标签: php laravel datetime compare php-carbon

如何比较这两个小时之间的小时数?

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

$last = EmergencyOrder::select('CreatedDate')
                        ->orderBy('CreatedDate', 'desc')
                        ->first();

4 个答案:

答案 0 :(得分:3)

来自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)

如果你需要差异

$today->diffInHours($last);
$today->diffInMinutes($last);
$today->diffInDays($last);

答案 1 :(得分:0)

这个答案的更好版本

$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);
}

如果失败,这将不会给出任何错误。

答案 2 :(得分:0)

//全部都有简短的别名和PHP等效代码:

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)

答案 3 :(得分: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)