🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# PHP Carbon 教程 > 原文: [https://zetcode.com/php/carbon/](https://zetcode.com/php/carbon/) PHP Carbon 教程展示了如何在带有 Carbon 包的 PHP 中使用日期和时间。 我们使用`nesbot/carbon`包。 ## PHP Carbon Carbon 是一个 PHP 日期时间库。 它是 PHP `DateTime`类的扩展。 ## PHP Carbon 设置 该包随 composer 一起安装。 ```php $ composer req nesbot/carbon ``` 我们安装`nesbot/carbon`包。 ## 今天的日期 以下示例显示如何获取今天的日期。 `today.php` ```php <?php require __DIR__ . '/vendor/autoload.php'; use Carbon\Carbon; $now = Carbon::now(); echo "$now\n"; $today = Carbon::today(); echo "$today\n"; ``` `Carbon::now()`返回当前日期和时间,`Carbon:today()`返回当前日期。 ```php $ php today.php 2019-05-07 10:20:54 2019-05-07 00:00:00 ``` 这是一个示例输出。 `Carbon::yesterday()`为昨天创建一个 Carbon 实例,为明天创建`Carbon::tomorrow()`。 `yes_tom.php` ```php <?php require __DIR__ . '/vendor/autoload.php'; use Carbon\Carbon; $yes = Carbon::yesterday(); echo "Yesterday: $yes\n"; $tom = Carbon::tomorrow(); echo "Tomorrow: $tom\n"; ``` 该示例显示了昨天和明天的日期。 ## Carbon 来创建 Carbon 实例可以使用几种创建方法来创建。 `create.php` ```php <?php require __DIR__ . "/vendor/autoload.php"; use Carbon\Carbon; $d1 = Carbon::create(2018, 8, 25, 22, 48, 00); echo $d1 . "\n"; $d2 = Carbon::create(2018, 8, 25, 22, 48, 00, 'Europe/Moscow'); echo $d2 . "\n"; $d3 = Carbon::createFromDate(2018, 8, 14, 'America/Chicago'); echo $d3 . "\n"; $d4 = Carbon::createFromTimestamp(1); echo $d4 . "\n"; ``` 该示例使用四种不同的方法创建 Carbon 实例。 ```php $d1 = Carbon::create(2018, 8, 25, 22, 48, 00); ``` `create()`方法根据日期和时间部分生成一个 Carbon 实例。 ```php $d2 = Carbon::create(2018, 8, 25, 22, 48, 00, 'Europe/Moscow'); ``` 在第二个示例中,我们还提供了一个时区。 ```php $d3 = Carbon::createFromDate(2018, 8, 14, 'America/Chicago'); ``` 使用`createFromDate()`,我们创建一个带有日期部分的 Carbon 实例。 ```php $d4 = Carbon::createFromTimestamp(1); ``` `createFromTimestamp()`从 Unix 时间创建一个 Carbon 实例。 ```php $ php create.php 2018-08-25 22:48:00 2018-08-25 22:48:00 2018-08-14 03:33:16 1970-01-01 01:00:01 ``` This is a sample output. ## Carbon 相对修饰符 碳实例可以通过相对修饰符来创建,例如下一个星期五或一年之前。 `relative_modifiers.php` ```php <?php require __DIR__ . "/vendor/autoload.php"; use Carbon\Carbon; echo new Carbon('tomorrow') . "\n"; echo new Carbon('yesterday') . "\n"; echo new Carbon('next wednesday') . "\n"; echo new Carbon('last friday') . "\n"; echo new Carbon('this saturday') . "\n"; echo new Carbon('1 year ago') . "\n"; ``` 该示例使用几个修饰符创建 Carbon 实例。 ```php $ php relative_modifiers.php 2019-05-08 00:00:00 2019-05-06 00:00:00 2019-05-08 00:00:00 2019-05-03 00:00:00 2019-05-11 00:00:00 2018-05-07 11:53:03 ``` This is a sample output. ## Carbon `next()`和`previous()` `next()`和`previous()`方法给出一周中给定日期的下一个/上一个出现。 `next_previous.php` ```php <?php require __DIR__ . "/vendor/autoload.php"; use Carbon\Carbon; $now = Carbon::now(); echo "$now\n"; $next_monday = $now->next(Carbon::MONDAY); echo "Next monday: $next_monday\n"; $prev_monday = $now->previous(Carbon::MONDAY); echo "Previous monday: $prev_monday\n"; ``` 该示例显示了下一个和上一个星期一。 ```php $ php next_previous.php 2019-05-07 11:34:32 Next monday: 2019-05-13 00:00:00 Previous monday: 2019-05-06 00:00:00 ``` This is a sample output. ## Carbon 日期时间 Carbon 日期时间包括年,月,日或小时。 `parts.php` ```php <?php require __DIR__ . '/vendor/autoload.php'; use Carbon\Carbon; $now = Carbon::now(); echo $now->year . "\n"; echo $now->month . "\n"; echo $now->day . "\n"; echo $now->hour . "\n"; echo $now->second . "\n"; echo $now->dayOfWeek . "\n"; echo $now->dayOfYear . "\n"; echo $now->weekOfMonth . "\n"; echo $now->daysInMonth . "\n"; ``` 该示例显示 Carbon 日期时间实例的各个部分。 ```php $ php parts.php 2019 5 7 10 31 2 126 1 31 ``` This is a sample output. ## Carbon 流式 API Carbon 还提供了方便的流式 API 来处理日期时间。 `fluent_api.php` ```php <?php require __DIR__ . "/vendor/autoload.php"; use Carbon\Carbon; $dt = Carbon::create(); $dt->year(2019)->month(5)->day(6)->hour(16)->minute(12)->second(53); echo $dt . "\n"; $dt2 = Carbon::create(); $dt2->setDate(2019, 5, 6)->setTime(16, 12, 53); echo $dt2 . "\n"; $dt3 = Carbon::create(); $dt3->setDate(2019, 5, 6)->setTime(16, 12, 53); echo $dt3 . "\n"; ``` 该示例使用流畅的 API 创建了三个 Carbon 实例。 ```php $ php fluent_api.php 2019-05-06 16:12:53 2019-05-06 16:12:53 2019-05-06 16:12:53 ``` 这是输出。 ## Carbon 修改方法 Carbon 修改方法修改实例。 我们可以使用`copy()`方法来处理副本。 `copy_method.php` ```php <?php require __DIR__ . "/vendor/autoload.php"; use Carbon\Carbon; echo "Tomorrow: " . Carbon::tomorrow() . "\n"; echo "*************************\n"; $dt = new Carbon('tomorrow'); echo $dt->subDay() . "\n"; echo $dt . "\n"; echo "*************************\n"; $dt2 = new Carbon('tomorrow'); echo $dt2->copy()->subDay() . "\n"; echo $dt2 . "\n"; ``` 该示例介绍了`copy()`方法。 ```php $ php copy_method.php Tomorrow: 2019-05-08 00:00:00 ************************* 2019-05-07 00:00:00 2019-05-07 00:00:00 ************************* 2019-05-07 00:00:00 2019-05-08 00:00:00 ``` 在第二种情况下,原始实例是完整的。 ## 碳加减法 碳提供了两种不同的方法来轻松增加和减少时间。 `add_sub.php` ```php <?php require __DIR__ . '/vendor/autoload.php'; use Carbon\Carbon; $now = Carbon::now(); echo "$now\n"; $d1 = $now->copy()->addDays(3); echo "$d1\n"; $d2 = $now->copy()->addHours(12); echo "$d2\n"; $d3 = $now->copy()->subDays(3); echo "$d3\n"; $d4 = $now->copy()->subHours(12); echo "$d4\n"; ``` 该示例介绍了`addDays()`,`addHours()`,`subDays()`和`subHours()`方法。 ```php $ php add_sub.php 2019-05-07 12:27:22 2019-05-10 12:27:22 2019-05-08 00:27:22 2019-05-04 12:27:22 2019-05-07 00:27:22 ``` This is a sample output. 下面的示例显示其他加法和减法。 `add_sub.php` ```php <?php require __DIR__ . '/vendor/autoload.php'; use Carbon\Carbon; $now = Carbon::now(); $d1 = $now->copy()->addCenturies(2); echo $d1->toDateString() . "\n"; $d2 = $now->copy()->subCenturies(2); echo $d2->toDateString() . "\n"; $d3 = $now->copy()->addYears(2); echo $d3->toDateString() . "\n"; $d4 = $now->copy()->subYears(2); echo $d4->toDateString() . "\n"; $d5 = $now->copy()->addMonths(2); echo $d5->toDateString() . "\n"; $d6 = $now->copy()->subMonths(2); echo $d6->toDateString() . "\n"; ``` 我们显示`addCenturies()`,`subCenturies()`,`addYears()`,`subYears()`,`addMonths()`和`subMonths()`。 ```php $ php add_sub2.php 2219-05-07 1819-05-07 2021-05-07 2017-05-07 2019-07-07 2019-03-07 ``` This is a sample output. ## Carbon 格式化日期时间 Carbon 提供了几种格式化日期时间的方法。 `formatting.php` ```php <?php require __DIR__ . "/vendor/autoload.php"; use Carbon\Carbon; $dt = Carbon::now(); echo $dt . "\n"; echo $dt->toDateTimeString(). "\n"; echo "******************************\n"; echo $dt->toDateString(). "\n"; echo $dt->toFormattedDateString(). "\n"; echo $dt->toTimeString(). "\n"; echo $dt->toDayDateTimeString(). "\n"; echo "******************************\n"; echo $dt->format('Y-m-d h:i:s A'). "\n"; ``` 该示例提供了基本的格式化方法。 我们还可以使用`format()`生成自定义格式。 ```php $ php formatting.php 2019-05-07 10:36:09 2019-05-07 10:36:09 ****************************** 2019-05-07 May 7, 2019 10:36:09 Tue, May 7, 2019 10:36 AM ****************************** 2019-05-07 10:36:09 AM ``` This is a sample output. 下面的示例显示其他常见的 Carbon 日期时间格式化方法。 `common_formats.php` ```php <?php require __DIR__ . "/vendor/autoload.php"; use Carbon\Carbon; $dt = Carbon::createFromFormat('Y-m-d H:i:s.u', '2019-05-06 16:45:00.613484'); echo $dt->toAtomString() . "\n"; echo $dt->toCookieString() . "\n"; echo $dt->toIso8601String() . "\n"; echo $dt->toIso8601ZuluString() . "\n"; echo $dt->toRfc822String() . "\n"; echo $dt->toRfc850String() . "\n"; echo $dt->toRfc1036String() . "\n"; echo $dt->toRfc1123String() . "\n"; echo $dt->toRfc3339String() . "\n"; echo $dt->toRfc7231String() . "\n"; echo $dt->toRssString() . "\n"; echo $dt->toW3cString() . "\n"; ``` 该示例介绍了其他十二种方法。 ```php $ php common_formats.php 2019-05-06T16:45:00+02:00 Monday, 06-May-2019 16:45:00 CEST 2019-05-06T16:45:00+02:00 2019-05-06T14:45:00Z Mon, 06 May 19 16:45:00 +0200 Monday, 06-May-19 16:45:00 CEST Mon, 06 May 19 16:45:00 +0200 Mon, 06 May 2019 16:45:00 +0200 2019-05-06T16:45:00+02:00 Mon, 06 May 2019 14:45:00 GMT Mon, 06 May 2019 16:45:00 +0200 2019-05-06T16:45:00+02:00 ``` This is the output. ## Carbon 比较日期时间 Carbon 具有比较日期时间的方法,例如`eq()`和`gt()`。 `comparing.php` ```php <?php require __DIR__ . "/vendor/autoload.php"; use Carbon\Carbon; $first = Carbon::create(2019, 5, 5, 22, 20, 1); $second = Carbon::create(2019, 5, 5, 20, 20, 1); echo $first . "\n"; echo $second . "\n"; var_dump($first->eq($second)); var_dump($first->ne($second)); var_dump($first->gt($second)); var_dump($first->gte($second)); var_dump($first->lt($second)); var_dump($first->lte($second)); ``` 该示例比较两个日期时间值。 ```php $ php comparison.php 2019-05-05 22:20:01 2019-05-05 20:20:01 bool(false) bool(true) bool(true) bool(true) bool(false) bool(false) ``` This is the output. ## Carbon UTC 协调世界时(UTC)是世界各地用来调节时钟和时间的主要时间标准。 `utc.php` ```php <?php require __DIR__ . "/vendor/autoload.php"; use Carbon\Carbon; $now = Carbon::now(); echo "$now\n"; isUtc($now); echo "Offset hours: {$now->offsetHours}\n"; echo "******************************\n"; $now->tz('UTC'); echo "$now\n"; isUtc($now); function isUtc($now): void { if ($now->utc) { echo "Datetime is in UTC\n"; } else { echo "Datetime is not in UTC\n"; } } ``` 该示例计算 UTC 时间(以小时为单位的偏移量),并确定日期时间是否为 UTC 时间。 ```php $ php utc.php 2019-05-07 11:14:18 Datetime is not in UTC Offset hours: 2 ****************************** 2019-05-07 09:14:18 Datetime is in UTC ``` This is a sample output. ## Carbon 人性化日期时间差异 应用通常以所谓的人性化格式显示日期时间差异。 例如在一年或三分钟前。 `humanized.php` ```php <?php require __DIR__ . "/vendor/autoload.php"; use Carbon\Carbon; echo Carbon::now()->addYear()->diffForHumans() . "\n"; Carbon::setLocale('de'); echo Carbon::now()->addYear()->diffForHumans() . "\n"; Carbon::setLocale('sk'); echo Carbon::now()->addYear()->diffForHumans() . "\n"; ``` 该示例显示了三种语言环境中的日期时间差异。 ```php $ php humanize.php 1 year from now in 1 Jahr za rok ``` This is the output. ## Carbon 修饰符 修饰符方法对当前实例执行有用的修改。 他们可以检索周,月或年的开始/结束。 `modifiers.php` ```php <?php require __DIR__ . "/vendor/autoload.php"; use Carbon\Carbon; echo "Start/End of day\n"; $dt = Carbon::create(2019, 5, 6, 12, 0, 0); echo $dt->copy()->startOfDay() . "\n"; echo $dt->copy()->endOfDay() . "\n"; echo "\nStart/End of month\n"; echo $dt->copy()->startOfMonth() . "\n"; echo $dt->copy()->endOfMonth() . "\n"; echo "\nStart/End of year\n"; echo $dt->copy()->startOfYear() . "\n"; echo $dt->copy()->endOfYear() . "\n"; echo "\nStart/End of decade\n"; echo $dt->copy()->startOfDecade() . "\n"; echo $dt->copy()->endOfDecade() . "\n"; echo "\nStart/End of century\n"; echo $dt->copy()->startOfCentury() . "\n"; echo $dt->copy()->endOfCentury() . "\n"; ``` 该示例提出了几个修饰符。 ```php $ php modifiers.php Start/End of day 2019-05-06 00:00:00 2019-05-06 23:59:59 Start/End of month 2019-05-01 00:00:00 2019-05-31 23:59:59 Start/End of year 2019-01-01 00:00:00 2019-12-31 23:59:59 Start/End of decade 2010-01-01 00:00:00 2019-12-31 23:59:59 Start/End of century 2001-01-01 00:00:00 2100-12-31 23:59:59 ``` This is a sample output. 在本教程中,我们使用 PHP Carbon 来处理日期和时间。 您可能也对以下相关教程感兴趣: [PHP PDO 教程](/php/pdo/), [Twig 教程](/php/twig/)和 [PHP 教程](/lang/php/),或列出[所有 PHP 教程](/all/#php) ]。