Carbonチートシート

# [Carbon](https://carbon.nesbot.com/)
日時の操作に便利なライブラリ
[Illuminate\Support\Carbon](https://laravel.com/api/8.x/Illuminate/Support/Carbon.html) は [Carbon\Carbon](https://github.com/briannesbitt/carbon) を継承したクラスだけど昔ほど差は無い

## Carbonをインストールする
```
composer require nesbot/carbon
```

## Carbonを作る
```php
$carbon = new Carbon(); // 2021-03-28 08:17:02.064073
$carbon = new Carbon('today'); // 2021-03-28 00:00:00.0
$carbon = new Carbon('2021-03-28'); // 2021-03-28 00:00:00.0
$carbon = new Carbon('2021-03-28 12:34:56.777777'); // 2021-03-28 12:34:56.777777
$carbon = Carbon::now(); // 2021-03-28 08:17:02.064073
$carbon = Carbon::today(); // 2021-03-28 00:00:00.0
$carbon = Carbon::create(2021, 3, 28, 12, 34, 56.777777); // 2021-03-28 12:34:56.777777
$carbon = Carbon::parse('today'); // 2021-03-28 00:00:00.0
$carbon = Carbon::parse('2021-03-28 12:34:56'); // 2021-03-28 12:34:56.0
$carbon = Carbon::createFromTime(12, 34, 56); // 2021-03-28 12:34:56.0
$carbon = Carbon::createFromTimeString('12:34:56'); // 2021-03-28 12:34:56.0
$carbon = Carbon::createFromFormat('Y-m-d H:i', '2021-03-28 12:34'); // 2021-03-28 12:34:00.0
$carbon = Carbon::createFromTimestamp(1616887022); // 2021-03-28 08:17:02

$carbon2 = $carbon->copy(); // コピー
```

## Carbonを操作する
```php
$carbon = new Carbon(); // 2021-01-30 08:17:02.064073
$carbon->addYears(1); // 2022-01-30 08:17:02.064073
$carbon->addMonths(1); // 2021-03-02 08:17:02.064073
$carbon->addMonthsWithOverflow(1); // 2021-03-02 08:17:02.064073
$carbon->addMonthsNoOverflow(1); // 2021-02-28 08:17:02.064073
$carbon->addDays(1); // 2021-01-31 08:17:02.064073
$carbon->addHours(1); // 2021-01-30 09:17:02.064073
$carbon->addMinutes(1); // 2021-01-30 08:18:02.064073
$carbon->addSeconds(1); // 2021-01-30 08:17:03.064073
$carbon->startOfMonth(); // 2021-01-01 00:00:00.0
$carbon->startOfDay(); // 2021-01-30 00:00:00.0
$carbon->startOfHour(); // 2021-01-30 08:00:00.0
$carbon->startOfMinute(); // 2021-01-30 08:17:00.0
$carbon->endOfMonth(); // 2021-01-31 23:59:59.999999
$carbon->endOfDay(); // 2021-01-30 23:59:59.999999
$carbon->endOfHour(); // 2021-01-30 08:59:59.999999
$carbon->endOfMinute(); // 2021-01-30 08:17:59.999999
```

## Carbonを設定する
```php
$carbon = new Carbon(); // 2021-01-30 08:17:02.064073
$carbon->year = 2020; // 2020-01-30 08:17:02.064073
$carbon->year(2020); // 2020-01-30 08:17:02.064073
$carbon->timestamp(1616887022); // 2021-01-30 08:17:02.0
$carbon->setDate(2021, 3, 28); // 2021-03-28 08:17:02.064073
$carbon->setTime(12, 34, 56); // 2021-01-30 12:34:56.064073
$carbon->setDateTime(2021, 3, 28, 12, 34, 56); // 2021-03-28 12:34:56.064073
```

## Carbonを変換する
```php
$carbon = new Carbon(); // 2021-01-30 08:17:02.064073
$carbon->toDateString(); // "2021-01-30"
$carbon->toTimeString(); // "08:17:02"
$carbon->toDateTimeString(); // "2021-01-30 08:17:02"
$carbon->toAtomString(); // "2021-01-30T08:17:02+9:00"
$carbon->format('Y年m月d日 H時i分s秒'); // "2021年01月30日 08時17分02秒"
```

## Carbonを判定する
```php
$carbon->isToday();
$carbon->isFuture();
$carbon->isPast();
$carbon->isCurrentYear();
$carbon->isNextYear();
$carbon->isLastYear();
$carbon1->isSameYear($carbon2);
$carbon3->between($carbon1, $carbon2); // $carbon1 <= $carbon3 <= $carbon2
$carbon1->eq($carbon2); // $carbon1 == $carbon2
$carbon1->ne($carbon2); // $carbon1 != $carbon2
$carbon1->gt($carbon2); // $carbon1 > $carbon2
$carbon1->gte($carbon2); // $carbon1 >= $carbon2
$carbon1->lt($carbon2); // $carbon1 < $carbon2
$carbon1->lte($carbon2); // $carbon1 <= $carbon2
```