Laravel日期处理

1. 常用:

echo Carbon::now();                             // 2023-04-08 18:07:24
echo Carbon::today();                           // 2023-04-08 00:00:00
echo  Carbon::tomorrow('Europe/London');        // 2023-04-09 00:00:00
echo Carbon::yesterday();                       // 2023-04-07 00:00:00

$dt = Carbon::create(2012, 1, 31, 12, 0, 0);
echo $dt->startOfDay(); // 2012-01-31 00:00:00
echo $dt->endOfDay(); // 2012-01-31 23:59:59
echo $dt->startOfMonth(); // 2012-01-01 00:00:00
echo $dt->endOfMonth(); // 2012-01-31 23:59:59
echo $dt->startOfYear(); // 2012-01-01 00:00:00
echo $dt->endOfYear(); // 2012-12-31 23:59:59
echo $dt->startOfDecade(); // 2010-01-01 00:00:00
echo $dt->endOfDecade(); // 2019-12-31 23:59:59
echo $dt->startOfCentury(); // 2000-01-01 00:00:00
echo $dt->endOfCentury(); // 2099-12-31 23:59:59
echo $dt->startOfWeek(); // 2012-01-30 00:00:00
echo $dt->endOfWeek(); // 2012-02-05 23:59:59

2. 转为字符串

echo Carbon::now()->toDateString();     //2016-10-14
echo Carbon::now()->toDateTimeString(); //2016-10-14 20:22:50
echo Carbon::now()->format('Y-m-t'); //2016-10-31

3.解析

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

4.修改

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

5.比较

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)
判断一个日期是否介于两个日期之间,可以使用 between() 方法
$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)

6.返回两时间之间的天数

echo Carbon::parse('2023-04-05')->diffInDays('2023-09-02');//150
echo Carbon::parse('2021-04-05')->diffInYears('2023-09-02');//2
echo Carbon::now()->subDays(5)->diffForHumans(); // 5 days ago
echo Carbon::now()->addYear()->diffForHumans(); // 一年前

7.返回两时间之间的日期

$period = \Carbon\CarbonPeriod::create('2023-04-05', '2023-09-02');
$data = [];
foreach ($period as $key => $date) {
    $data[] = $date->format('Y-m-d');
}
var_dump($data);//[2023-04-05,2023-04-06,2023-04-07,2023-04-08,...,2023-09-02]

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

posted @ 2023-04-08 18:04  成文的博客  阅读(197)  评论(0编辑  收藏  举报