// $dt = new Carbon(); // $dt = Carbon::now(); // $dt = new Carbon('2016-12-20 11:32:23'); //$dt = new Carbon('tomorrow'); //$dt = Carbon::today(); //$dt = Carbon::tommorow(); // $dt = Carbon::yesterday(); // $dt = Carbon::parse('2016-12-12 11:32:32'); // $dt = Carbon::create(2020,12, 12, 11, 32, 32); $dt = Carbon::createFromFormat('Y/m/d H', '2016/12/20 20');
日時情報の取得
$dt = Carbon::now(); //echo $dt->year . PHP_EOL; //echo $dt->month . PHP_EOL; //echo $dt->day . PHP_EOL; //echo $dt->hour . PHP_EOL; //echo $dt->minute . PHP_EOL; //echo $dt->second . PHP_EOL; //echo $dt->dayOfWeek . PHP_EOL; //echo $dt->dayOfYear . PHP_EOL; //echo $dt->weekOfMonth . PHP_EOL; //echo $dt->weekOfYear . PHP_EOL; // echo $dt->timestamp . PHP_EOL; // echo $dt->tzName . PHP_EOL; echo $dt->format('Y年m月d日') . PHP_EOL;
日付判定
//var_dump($dt->isToday()) . PHP_EOL; //var_dump($dt->isTommorow()) . PHP_EOL; //var_dump($dt->isYesterday()) . PHP_EOL; //var_dump($dt->isFuture()) . PHP_EOL; //var_dump($dt->isPast()) . PHP_EOL; //var_dump($dt->isLeapYear()) . PHP_EOL; //var_dump($dt->isWeekday()) . PHP_EOL; //var_dump($dt->isWeekend()) . PHP_EOL; //var_dump($dt->isSameDay(Carbon::now())) . PHP_EOL;
変数をとって、日時を比較
$dt1 = Carbon::create(2020, 10, 1); $dt2 = Carbon::create(2020, 11, 1); // var_dump($dt1->eq($dt2)) . PHP_EOL; //var_dump($dt1->gt($dt2)) . PHP_EOL; //var_dump($dt1->lt($dt2)) . PHP_EOL; //var_dump(Carbon::create(2010, 10, 10)->between($dt1, $dt2)) . PHP_EOL; //echo $dt1->max($dt2) . PHP_EOL; //echo $dt1->min($dt2) . PHP_EOL;
日付計算
// echo $dt->addYear(). PHP_EOL; //echo $dt->addYears(3). PHP_EOL; //echo $dt->subYears(3). PHP_EOL; //echo $dt->addWeekdays(3). PHP_EOL; //echo $dt->addWeekdays(3)->addWeekHours(3)->addWeekMinutes(3). PHP_EOL;
コピーメソッド
$dt = Carbon::now(); echo $dt->copy()->addYear() . PHP_EOL; echo $dt . PHP_EOL;
日時指定
//echo $dt->startOfDay() . PHP_EOL; //echo $dt->endOfDay() . PHP_EOL; //echo $dt->startOfMonth() . PHP_EOL; //echo $dt->endOfMonth() . PHP_EOL; //echo $dt->startOfWeek() . PHP_EOL; //echo $dt->endOfWeek() . PHP_EOL; //echo $dt->next(Carbon::MONDAY) . PHP_EOL; //echo $dt->previous(Carbon::MONDAY) . PHP_EOL; //echo $dt->firstOfMonth(Carbon::MONDAY) . PHP_EOL; //echo $dt->lastOfMonth(Carbon::MONDAY) . PHP_EOL; //echo $dt->nthOfMonth(3, Carbon::MONDAY) . PHP_EOL;
日時の差
$dt1 = Carbon::create(2020, 10, 1); $dt2 = Carbon::create(2020, 11, 1); //echo $dt1->diffInDays($dt2) . PHP_EOL; //echo $dt1->diffInHours($dt2) . PHP_EOL; //echo $dt1->diffInMinutes($dt2) . PHP_EOL; //echo $dt1->diffForHumans($dt2) . PHP_EOL;
setTestNow()を使った判定
$dt = Carbon::now(); $birthday = Carbon::create(1990, 10, 1); Carbon::setTestNow(Carbon::create(2020, 10, 1)); echo Carbon::now() . PHP_EOL; if($birthday->isBirthday(Carbon::now())){ echo ':)' . PHP_EOL; } else { echo ':<' . PHP_EOL;