Carbon:A simple PHP API extension for DateTime.
文档
https://carbon.nesbot.com/docs/
https://github.com/briannesbitt/carbon
安装
$ composer require nesbot/carbon
1
$ composer require nesbot/carbon
示例
<?php require './vendor/autoload.php'; use Carbon\Carbon; // 1、获取时间 $now = Carbon::now(); print_r($now); // ( // [date] => 2022-01-11 15:30:42.927812 // [timezone_type] => 3 // [timezone] => Asia/Shanghai // ) // 2、解析时间 $date_time = Carbon::parse('2022-01-16 15:25:26'); // 3、格式化 print_r($date_time->toDateTimeString(). PHP_EOL); // 2022-01-16 15:25:26 print_r($date_time->toDateString() . PHP_EOL); // 2022-01-16 print_r($date_time->toTimeString() . PHP_EOL); // 15:25:26 print_r($date_time->format('Y-m-d H:i:s') . PHP_EOL); // 2022-01-16 15:25:26 // 4、时间加减 $new_day = $date_time->addDays(5); print_r($new_day->toDateTimeString() . PHP_EOL); // 2022-01-21 15:25:26 $new_day = $date_time->subDays(5); print_r($new_day->toDateTimeString() . PHP_EOL); // 2022-01-16 15:25:26 // 5、时间比较 $date_time->greaterThan($new_day); // bool $date_time->lessThan($new_day); // bool // 6、时间计算(计算两个时间之间相差几分钟) $date_time1 = Carbon::parse('2022-01-16 15:25:26'); $date_time2 = Carbon::parse('2022-01-16 15:48:26'); echo $date_time2->diffInMinutes($date_time1); // 23