书香隽秀 - Carbon
type: channel
link: https://www.huangshuxiu.com/Carbon/
description: Carbon
items: Carbon文档
items: Carbon常用方法

Carbon文档
type: item
date: 2023-05-17T20:55:00+08:00
description: Introduction<?php namespace Carbon; class Carbon extends \DateTime { // code here }use Carbon\Carbon;We also provide CarbonImmutable class extending DateTimeImmutable. The same methods are available on both classes but when you use a modifier on a Carbon instance, it modifies and returns the same instance, when you use it on CarbonImmutable, it returns a new instances with the new value.<?php $mutable = Carbon::now(); $immutable = CarbonImmutable::now(); $modifiedMutable = $mutable->add(1, 'day'); $modifiedImmutable = CarbonImmutable::now()->add(1, 'day'); var_dump($modifiedMutable === $mutable); // bool(true) var_dump($mutable->isoFormat('dddd D')); // string(11) "Saturday 22" var_dump($modifiedMutable->isoFormat('dddd D')); // string(11) "Saturday 22" // So it means $mutable and $modifiedMutable are the same object // both set to now + 1 day. var_dump($modifiedImmutable === $immutable); // bool(false) var_dump($immutable->isoFormat('dddd D')); // string(9) "Friday 21" var_dump($modifiedImmutable->isoFormat('dddd D')); // string(11) "Saturday 22" // While $immutable is still set to now and cannot be changed and // $modifiedImmutable is a new instance created from $immutable // set to now + 1 day. $mutable = CarbonImmutable::now()->toMutable(); var_dump($mutable->isMutable()); // bool(true) var_dump($mutable->isImmutable()); // bool(false) $immutable = Carbon::now()->toImmutable(); var_dump($immutable->isMutable()); // bool(false) var_dump($immutable->isImmutable()); // bool(true)Carbon has all of the functions inherited from the base DateTime class. This approach allows you to access the base functionality such as modify, format or diff.<?php $dtToronto = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Toronto'); $dtVancouver = Carbon::create(2012, 1, 1, 0, 0, 0, 'America/Vancouver'); // Try to replace the 4th number (hours) or the last argument (timezone) with // Europe/Paris for example and see the actual result on the right hand. // It's alive! echo $dtVancouver->d…

Carbon常用方法
type: item
date: 2023-05-16T10:26:00+08:00
description: use Carbon\Carbon; // Carbon 类在 Carbon 命名空间中声明 // 获取当前时间 echo Carbon::now(); // 2017-04-07 16:32:28 // 今天 echo Carbon::today(); // 2017-04-07 00:00:00 // 明天 echo Carbon::tomorrow(); // 2017-04-08 00:00:00 // 昨天 echo Carbon::yesterday(); // 2017-04-06 00:00:00 // 获取前一天的开始时间 echo Carbon::now()->yesterday()->startOfDay(); // 2017-04-06 00:00:00 // 获取前一天的结束时间 echo Carbon::now()->yesterday()->endOfDay(); // 2017-04-06 23:59:59 //上一周开始时间 (星期一) echo Carbon::now()->previous()->startOfWeek(); // 2017-03-27 00:00:00 // 这周周一 echo Carbon::now()->startOfWeek(); // 2017-04-03 00:00:00 //上一周结束时间 (星期天) echo Carbon::now()->previous()->endOfWeek(); // 2017-04-02 23:59:59 // 这周周天 echo Carbon::now()->endOfWeek(); // 2017-04-09 23:59:59 //获取上一月的开始时间 echo Carbon::now()->subMonth(1)->startOfMonth(); // 2017-03-01 00:00:00 // 获取上一月的结束时间 echo Carbon::now()->subMonth(1)->endOfMonth(); // 2017-03-31 23:59:59 // 这个月的第一天开始时间 echo Carbon::now()->startOfMonth(); // 2017-04-01 00:00:00 // 这个月的最后一天结束时间 echo Carbon::now()->endOfMonth(); // 2017-04-30 23:59:59 // 获取上一年的开始时间 echo Carbon::now()->subYear(1)->startOfYear(); // 2016-01-01 00:00:00 // 今年开始时间 echo Carbon::now()->startOfYear(); // 2017-01-01 00:00:00 echo Carbon::now()->lastOfYear()->startOfYear(); // 2017-01-01 00:00:00 // 获取上一年的结束时间 echo Carbon::now()->subYear(1)->endOfYear(); // 2016-12-31 23:59:59 // 今年结束时间 echo Carbon::now()->endOfYear(); // 2017-12-31 23:59:59 echo Carbon::now()->lastOfYear()->endOfYear(); // 2017-12-31 23:59:59 // 获取当前时间的固定格式 echo Carbon::now()->format('Y-m-d'); // 2017-04-07 // 几号 echo Carbon::now()->day; // 7 // 几月 echo Carbon::now()->month; // 4 // 年 echo Carbon::now()->year; // 2017 // 小时 echo Carbon::now()->hour; // 分钟 echo Carbon::now()->minute; // 上周的今天是星期几 echo Carbon::now()->dayOfWeek; // 5 // 今天是365天中的第几天 echo Carbon::now()->dayOfYear; // 96 // 这个月中的第几周 echo Carbon::now()->weekOfMonth; // 1 // 年中的第几周 echo Carbon::now()->weekOfYear; // 14 // 季度 echo Carbon::now()->quarter; // 2 // 几岁 echo Carbon::createFromDa…