$carbon = \Illuminate\Support\Carbon::now('Asia/shanghai');
$carbon = \Illuminate\Support\Carbon::now( new DateTimeZone('Asia/shanghai') );
today() , tomorrow() , yesterday() 返回值 Carbon对象
\Carbon\Carbon::today('Asia/shanghai');
\Carbon\Carbon::tomorrow('Asia/shanghai');
\Carbon\Carbon::yesterday('Asia/shanghai');
字符串类型输出
echo \Carbon\Carbon::now('Asia/shanghai')->toDateTimeString();
echo \Carbon\Carbon::now('Asia/shanghai')->toDateString();
echo \Carbon\Carbon::parse('2 days ago', 'UTC')->format('Y-m-d H:i');
解析已有时间
\Carbon\Carbon::parse('2019-01-01 01:01:01', 'UTC');
\Carbon\Carbon::parse('2019-01-01');
\Carbon\Carbon::parse('today');
\Carbon\Carbon::parse('tomorrow');
\Carbon\Carbon::parse('yesterday');
\Carbon\Carbon::parse('2 days ago') ;
\Carbon\Carbon::parse('+3 days');
\Carbon\Carbon::parse('+2 weeks');
\Carbon\Carbon::parse('+4 months');
\Carbon\Carbon::parse('-1 year');
\Carbon\Carbon::parse('next wednesday');
\Carbon\Carbon::parse('last friday');
\Carbon\Carbon::createFromDate(2018,12,12, 'Asia/shanghai');
\Carbon\Carbon::create(2018,12,12, 12, 12,12,'Asia/shanghai');
\Carbon\Carbon::createFromDate(2018,12,12, 'Asia/shanghai');
\Carbon\Carbon::createFromDate(null,12,12, 'Asia/shanghai');
\Carbon\Carbon::createFromDate(null,10,null, 'Asia/shanghai');
日期操作 addXxx()
\Carbon\Carbon::now('Asia/shanghai');
\Carbon\Carbon::now('Asia/shanghai')->addDay();
\Carbon\Carbon::now('Asia/shanghai')->addDays(5);
\Carbon\Carbon::now('Asia/shanghai')->addWeek();
\Carbon\Carbon::now('Asia/shanghai')->addWeeks(3);
\Carbon\Carbon::now('Asia/shanghai')->addHour();
\Carbon\Carbon::now('Asia/shanghai')->addHours(3);
\Carbon\Carbon::now('Asia/shanghai')->addMinute();
\Carbon\Carbon::now('Asia/shanghai')->addMinutes(5);
\Carbon\Carbon::now('Asia/shanghai')->modify('-2 days');
\Carbon\Carbon::now('Asia/shanghai')->modify('+2 days');
日期操作 subXxx()
$one = \Carbon\Carbon::parse('2019-12-12 12:12:00');
$tow = \Carbon\Carbon::parse('2019-12-12 00:00:00');
dump( $one->subWeek() );
dump( $one->subWeeks(2) );
dump( $one->subWeekday() );
dump( $one->subWeekdays(3) );
dump( $one->subHour() );
dump( $one->subHours(4) );
dump( $one->subMinute() );
dump( $one->subMinutes(5) );
dump( $one->subSecond() );
dump( $one->subSeconds(23) );
dump( $one->subDay() );
dump( $one->subDays(2) );
dump( $one->subMonth() );
dump( $one->subMonths(2) );
dump( $one->subYear() );
dump( $one->subYears(2) );
dump( $one->subQuarter() );
dump( $one->subQuarters(2) );
dump( $one->subQuarters(3) );
dump( $one->subRealHour() );
dump( $one->subRealHours(2) );
dump( $one->subCentury() );
dump( $one->subCenturies(1) );
获取当前时区
echo \Carbon\Carbon::now('Asia/shanghai')->tzName;
Carbon 时区比较
$one = \Carbon\Carbon::parse('2019-12-12 12:12:00');
$tow = \Carbon\Carbon::parse('2019-12-12 00:00:00');
// eq 判断两个日期是否相等
$one->eq($tow); // false
// ne 判断两个日期否相等
$one->ne($tow); // true
// gt 判断第一个日期是否比第二个日期大
$one->gt($tow); // true
// lt 判断第一个日期是否比第二个日期小
$one->lt($tow); // false
// gte 判断第一个日期是否大于等于第二个日期
$one->gte($tow); // true
// lte 判断第一个日期是否小于等于第二个日期
$one->lte($tow); // false
// min 返回最小日期
$one->min($tow); // 2019-12-12 00:00:00
// max 返回最大日期
$one->max($tow); // 2019-12-12 12:12:00
betewwn 判断日期是否在两个日期之间
$one = \Carbon\Carbon::parse('2019-12-12 12:12:00');
$tow = \Carbon\Carbon::parse('2019-12-12 00:00:00');
// true
\Carbon\Carbon::parse('2019-12-12 08:00:00')->between($one, $tow);
// false
\Carbon\Carbon::parse('2019-12-12 14:00:00')->between($one, $tow);
// true
\Carbon\Carbon::parse('2019-12-12 12:12:00')->between($one, $tow);
// 第三个参数判断边界是否相等 默认true 默认边界可以相等
\Carbon\Carbon::parse('2019-12-12 12:12:00')->between($one, $tow, false); // false
$one = \Carbon\Carbon::parse('2019-12-12 12:12:00');
$tow = \Carbon\Carbon::parse('2019-12-12 00:00:00');
$one->isWeekday();
$one->isWeekend();
$tow->isYesterday();
$tow->isToday();
$tow->isTomorrow();
$tow->isFuture();
$tow->isPast();
$tow->isLeapYear();
时间显示 diffForHumans()
$one = \Carbon\Carbon::parse('2019-12-12 12:12:00');
dump( $one->subDays(6)->diffForHumans() );
dump( \Carbon\Carbon::now()->diffForHumans(\Carbon\Carbon::now()->subYear()) );
复制代码