ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## 日记时间4月21号 ### 时间格式化 ~~~ now()->format('Y-m-d') ~~~ ### 昨天 ~~~ now()->yesterday()->format('Y-m-d H:i:s') 或 Carbon::yesterday()->format('Y-m-d H:i:s') "2023-04-20 00:00:00" ~~~ ### 今天 ~~~ now()->startOfDay()->format('Y-m-d H:i:s') "2023-04-21 00:00:00" ~~~ ~~~ now()->endOfDay()->format('Y-m-d H:i:s'); "2023-04-21 23:59:59" ~~~ ### 几天前 ~~~ now()->subDays(7)->format('Y-m-d H:i:s') /*七天前*/ ~~~ ### 几天后 ~~~ now()->subDays(-2)->format('Y-m-d H:i:s') /*2天后*/ "2023-04-23 18:09:10" ~~~ ### 几个月前和几个月后 ~~~ now()->subMonth(3)->format('Y-m-d H:i:s') /*三个月前*/ "2023-01-21 18:11:09" now()->endOfMonth()->format('Y-m-d H:i:s') "2023-04-30 23:59:59" ~~~ ### 几年前和几年后 ~~~ now()->subYear(2)->format('Y-m-d H:i:s') /*默认一年, 两年前*/ ~~~ > 提示,now()->addDays() 也是 subDays() 一样我觉得,但是我没试过 now()->addDays() now()->addYear() now()->addMonth() now()->addSeconds() now()->addMinutes() ### 几天后 ~~~ now()->addDays(2)->format('Y-m-d H:i:s') /*2天后*/ "2023-04-23 18:29:14" ~~~ ~~~ 获取当前时间 Carbon::now()->toDateTimeString(); 获取下一天 Carbon::now()->addDays(1)->toDateTimeString(); //或 Carbon::tomorrow() 获取前一天 Carbon::now()->subDays(1)->toDateTimeString(); //或 Carbon::yesterday() 获取当月第一天 Carbon::now()->startOfMonth()->toDateTimeString(); 获取当前 年、月、日、时、分、秒、微秒 $date = Carbon::now(); $date->year; $date->month; $date->day; $date->hour; $date->minute; $date->second; $date->micro; 时间比较 $first = Carbon::parse('2019-08-01 15:29:46'); $second = Carbon::parse('2019-08-01 14:59:13'); // 等于 var_dump($first->eq($second)); // boolean false // 判断当前Carbon实例与指定Carbon对象时间是否不相同 var_dump($first->ne($second)); // boolean true // 大于 var_dump($first->gt($second)); // boolean true // 大于等于 var_dump($first->gte($second)); // boolean true // 小于 var_dump($first->lt($second)); // boolean false // 小于等于 var_dump($first->lte($second)); // boolean false ~~~ # 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、日期比较 ~~~ 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周 ~~~ ## 默认最近三个月数据查询 ~~~ # 日期 if (request('start_time') && request('end_time')) { $startTime = Carbon::parse(request('start_time'))->startOfDay()->toDateTimeString(); $endTime = Carbon::parse(request('end_time'))->endOfDay()->toDateTimeString(); $query->whereBetween('created_at', [$startTime, $endTime]); } # 默认最近三个月 $query->whereBetween('created_at', [ Carbon::now()->firstOfMonth()->subMonth(3)->toDateTimeString(), Carbon::now()->endOfDay()->toDateTimeString() ]); 打印示例 # 当前时间 Carbon::now()->toDateTimeString() $time = "2022-07-04 08:02:02"; # 2022-04-01 00:00:00 $start_time = Carbon::now()->firstOfMonth()->subMonth(3)->toDateTimeString(); # "2022-07-04 23:59:59" $end_time = Carbon::now()->endOfDay()->toDateTimeString(); ~~~ ~~~ 我需要找到两个日期之间的差异。假设我有 2017-02-01 - 2017-01-01。两天之间的天数就是输出 $formatted_dt1=Carbon::parse($a->date)->format('Y-m-d'); $formatted_dt2=Carbon::parse($c->dt)->format('Y-m-d'); $date_diff=$formatted_dt1->diffInDays($formatted_dt2); ~~~