快速切換前後日期
$now = Carbon::now();
echo $now; // 2015-03-26 00:36:47
$today = Carbon::today();
echo $today; // 2015-03-26 00:00:00
$tomorrow = Carbon::tomorrow('Europe/London');
echo $tomorrow; // 2015-03-27 00:00:00
$yesterday = Carbon::yesterday();
echo $yesterday; // 2015-03-25 00:00:00
## 建立特定日期的時間
$timezone = 'Asia/Taipei';
// 從「年月日」建立
Carbon::createFromDate($year, $month, $day, $timezone);
// 從「時分秒」建立
Carbon::createFromTime($hour, $minute, $second, $timezone);
// 從完整的「年月日時分秒」建立
Carbon::create($year, $month, $day, $hour, $minute, $second, $timezone);
// 從指定的格式建立
Carbon::createFromFormat($format, $time, $tz);
echo Carbon::createFromFormat('Y-m-d H', '1975-05-21 22')->toDateTimeString(); // 1975-05-21 22:00:00
// 從時間戳記建立
echo Carbon::createFromTimeStamp(-1)->toDateTimeString(); // 1969-12-31 18:59:59
echo Carbon::createFromTimeStamp(-1, 'Europe/London')->toDateTimeString(); // 1970-01-01 00:59:59
echo Carbon::createFromTimeStampUTC(-1)->toDateTimeString(); // 1969-12-31 23:59:59
## 轉換日期
透過文字移動日期
$knownDate = Carbon::create(2001, 5, 21, 12); // create testing date
Carbon::setTestNow($knownDate); // set the mock
echo new Carbon('tomorrow'); // 2001-05-22 00:00:00 ... notice the time !
echo new Carbon('yesterday'); // 2001-05-20 00:00:00
echo new Carbon('next wednesday'); // 2001-05-23 00:00:00
echo new Carbon('last friday'); // 2001-05-18 00:00:00
echo new Carbon('this thursday'); // 2001-05-24 00:00:00
## 格式化時間資料
$dt = Carbon::create(1975, 12, 25, 14, 15, 16);
var_dump($dt->toDateTimeString() == $dt); // bool(true) => uses __toString()
echo $dt->toDateString(); // 1975-12-25
echo $dt->toFormattedDateString(); // Dec 25, 1975
echo $dt->toTimeString(); // 14:15:16
echo $dt->toDateTimeString(); // 1975-12-25 14:15:16
echo $dt->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM
// 仍可以使用 format() 函式
echo $dt->format('l jS \of F Y h:i:s A'); // Thursday 25th of December 1975 02:15:16 PM
// 常用的時間格式
echo $dt->toAtomString(); // in 1 Jahr
echo $dt->toCookieString(); // Thursday, 25-Dec-1975 14:15:16 EST
echo $dt->toIso8601String(); // 1975-12-25T14:15:16-0500
echo $dt->toRfc822String(); // Thu, 25 Dec 75 14:15:16 -0500
echo $dt->toRfc850String(); // Thursday, 25-Dec-75 14:15:16 EST
echo $dt->toRfc1036String(); // Thu, 25 Dec 75 14:15:16 -0500
echo $dt->toRfc1123String(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $dt->toRfc2822String(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $dt->toRfc3339String(); // 1975-12-25T14:15:16-05:00
echo $dt->toRssString(); // Thu, 25 Dec 1975 14:15:16 -0500
echo $dt->toW3cString(); // 1975-12-25T14:15:16-05:00
## 時間運算
$dt = Carbon::create(2012, 1, 31, 0);
echo $dt->toDateTimeString(); // 2012-01-31 00:00:00
echo $dt->addYears(5); // 2017-01-31 00:00:00
echo $dt->addYear(); // 2018-01-31 00:00:00
echo $dt->subYear(); // 2017-01-31 00:00:00
echo $dt->subYears(5); // 2012-01-31 00:00:00
echo $dt->addMonths(60); // 2017-01-31 00:00:00
echo $dt->addMonth(); // 2017-03-03 00:00:00 equivalent of $dt->month($dt->month + 1); so it wraps
echo $dt->subMonth(); // 2017-02-03 00:00:00
echo $dt->subMonths(60); // 2012-02-03 00:00:00
echo $dt->addDays(29); // 2012-03-03 00:00:00
echo $dt->addDay(); // 2012-03-04 00:00:00
echo $dt->subDay(); // 2012-03-03 00:00:00
echo $dt->subDays(29); // 2012-02-03 00:00:00
echo $dt->addWeekdays(4); // 2012-02-09 00:00:00
echo $dt->addWeekday(); // 2012-02-10 00:00:00
echo $dt->subWeekday(); // 2012-02-09 00:00:00
echo $dt->subWeekdays(4); // 2012-02-03 00:00:00
echo $dt->addWeeks(3); // 2012-02-24 00:00:00
echo $dt->addWeek(); // 2012-03-02 00:00:00
echo $dt->subWeek(); // 2012-02-24 00:00:00
echo $dt->subWeeks(3); // 2012-02-03 00:00:00
echo $dt->addHours(24); // 2012-02-04 00:00:00
echo $dt->addHour(); // 2012-02-04 01:00:00
echo $dt->subHour(); // 2012-02-04 00:00:00
echo $dt->subHours(24); // 2012-02-03 00:00:00
echo $dt->addMinutes(61); // 2012-02-03 01:01:00
echo $dt->addMinute(); // 2012-02-03 01:02:00
echo $dt->subMinute(); // 2012-02-03 01:01:00
echo $dt->subMinutes(61); // 2012-02-03 00:00:00
echo $dt->addSeconds(61); // 2012-02-03 00:01:01
echo $dt->addSecond(); // 2012-02-03 00:01:02
echo $dt->subSecond(); // 2012-02-03 00:01:01
echo $dt->subSeconds(61); // 2012-02-03 00:00:00
本作品採用《CC 協議》,轉載必須註明作者和本文連結