laravel carbon时间处理类使用

参考文章:https://www.jianshu.com/p/9d2b576acded

1、获取当前时间

//可传递一个有效的时区作为参数
echo Carbon::now();//2019-08-17 09:27:49
echo Carbon::now('America/Los_Angeles');//2019-08-17 02:27:49

2、获取昨今明天

//可传递一个有效的时区作为参数
echo Carbon::today();//2019-08-17 00:00:00
echo  Carbon::tomorrow('America/Los_Angeles');//2019-08-18 00:00:00
echo Carbon::yesterday();//2019-08-16 00:00:00 

//以上结果都是对象

3、转字符串类型

echo Carbon::today()->toDateTimeString();
echo Carbon::yesterday()->toDateTimeString();
echo Carbon::tomorrow()->toDateTimeString();

4、日期解析

echo Carbon::parse('2016-10-15')->toDateTimeString(); //2016-10-15 00:00:00
echo Carbon::parse('2016-10-15')->toDateTimeString(); //2016-10-15 00:00:00
echo Carbon::parse('2016-10-15 00:10:25')->toDateTimeString(); //2016-10-15 00:10:25
 
echo Carbon::parse('today')->toDateTimeString(); //2016-10-15 00:00:00
echo Carbon::parse('yesterday')->toDateTimeString(); //2016-10-14 00:00:00
echo Carbon::parse('tomorrow')->toDateTimeString(); //2016-10-16 00:00:00
echo Carbon::parse('2 days ago')->toDateTimeString(); //2016-10-13 20:49:53
echo Carbon::parse('+3 days')->toDateTimeString(); //2016-10-18 20:49:53
echo Carbon::parse('+2 weeks')->toDateTimeString(); //2016-10-29 20:49:53
echo Carbon::parse('+4 months')->toDateTimeString(); //2017-02-15 20:49:53
echo Carbon::parse('-1 year')->toDateTimeString(); //2015-10-15 20:49:53
echo Carbon::parse('next wednesday')->toDateTimeString(); //2016-10-19 00:00:00
echo Carbon::parse('last friday')->toDateTimeString(); //2016-10-14 00:00:00

5、构造日期

$year = '2015';
$month = '04';
$day = '12';

echo Carbon::createFromDate($year, $month, $day); //2015-04-12 20:55:59
 
$hour = '02';
$minute = '15':
$second = '30';
 
echo Carbon::create($year, $month, $day, $hour, $minute, $second); //2015-04-12 02:15:30
 
echo Carbon::createFromDate(null, 12, 25);  // 年默认为当前年份

6、日期运算

//add-加 sub-减
echo Carbon::now()->addDays(25); //2016-11-09 14:00:01
echo Carbon::now()->addWeeks(3); //2016-11-05 14:00:01
echo Carbon::now()->addHours(25); //2016-10-16 15:00:01
echo Carbon::now()->subHours(2); //2016-10-15 12:00:01
echo Carbon::now()->addHours(2)->addMinutes(12); //2016-10-15 16:12:01
echo Carbon::now()->modify('+15 days'); //2016-10-30 14:00:01
echo Carbon::now()->modify('-2 days'); //2016-10-13 14:00:01

7、日期比较

/*
min –返回最小日期。
max – 返回最大日期。
eq – 判断两个日期是否相等。
gt – 判断第一个日期是否比第二个日期大。
lt – 判断第一个日期是否比第二个日期小。
gte – 判断第一个日期是否大于等于第二个日期。
lte – 判断第一个日期是否小于等于第二个日期。
*/

echo Carbon::now()->tzName;                        // America/Toronto
$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;                               // America/Toronto
echo $second->toDateTimeString();                  // 2012-09-05 20:26:11
echo $second->tzName;                              // America/Vancouver
 
var_dump($first->eq($second));                     // bool(true)
var_dump($first->ne($second));                     // bool(false)
var_dump($first->gt($second));                     // bool(false)
var_dump($first->gte($second));                    // bool(true)
var_dump($first->lt($second));                     // bool(false)
var_dump($first->lte($second));                    // bool(true)
 
$first->setDateTime(2012, 1, 1, 0, 0, 0);
$second->setDateTime(2012, 1, 1, 0, 0, 0);         // Remember tz is 'America/Vancouver'
 
var_dump($first->eq($second));                     // bool(false)
var_dump($first->ne($second));                     // bool(true)
var_dump($first->gt($second));                     // bool(false)
var_dump($first->gte($second));                    // bool(false)
var_dump($first->lt($second));                     // bool(true)
var_dump($first->lte($second));                    // bool(true)

8、判断一个日期是否介于两个日期之间

$first = Carbon::create(2012, 9, 5, 1);
$second = Carbon::create(2012, 9, 5, 5);
var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second));          // bool(true)
var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second));          // bool(true)
var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second, false));   // bool(false)

9、辅助方法

$dt = Carbon::now();
 
$dt->isWeekday();
$dt->isWeekend();
$dt->isYesterday();
$dt->isToday();
$dt->isTomorrow();
$dt->isFuture();
$dt->isPast();
$dt->isLeapYear();
$dt->isSameDay(Carbon::now());
$born = Carbon::createFromDate(1987, 4, 23);
$noCake = Carbon::createFromDate(2014, 9, 26);
$yesCake = Carbon::createFromDate(2014, 4, 23);
$overTheHill = Carbon::now()->subYears(50);
var_dump($born->isBirthday($noCake));              // bool(false)
var_dump($born->isBirthday($yesCake));             // bool(true)
var_dump($overTheHill->isBirthday());              // bool(true) -> default compare it to today!

10、将时间转为文字描述

echo Carbon::now()->subDays(5)->diffForHumans();               // 5天前
echo Carbon::now()->diffForHumans(Carbon::now()->subYear());   // 1年后
 
$dt = Carbon::createFromDate(2011, 8, 1);
echo $dt->diffForHumans($dt->copy()->addMonth());              // 1月前
echo $dt->diffForHumans($dt->copy()->subMonth());              // 11月后
 
echo Carbon::now()->addSeconds(5)->diffForHumans();            // 5秒距现在

echo Carbon::now()->subDays(24)->diffForHumans();              // 3周前
//可以把第二个参数设置为 true 来删除'前'、'距现在'等修饰语
echo Carbon::now()->subDays(24)->diffForHumans(null, true);    // 3周
posted @ 2019-08-17 18:18  pine007  阅读(2195)  评论(0编辑  收藏  举报