laravel Carbon函数
原文地址:https://blog.csdn.net/lbwo001/article/details/53063867
carbon官方网站:https://carbon.nesbot.com/docs/#api-settersfluent
快速切換前後日期
1 <?php 2 3 use Carbon\Carbon; 4 5 $now = Carbon::now(); 6 echo $now; // 2015-03-26 00:36:47 7 $today = Carbon::today(); 8 echo $today; // 2015-03-26 00:00:00 9 $tomorrow = Carbon::tomorrow('Europe/London'); 10 echo $tomorrow; // 2015-03-27 00:00:00 11 $yesterday = Carbon::yesterday(); 12 echo $yesterday; // 2015-03-25 00:00:00
建立特定日期的時間
1 <?php 2 3 use Carbon\Carbon; 4 5 $timezone = 'Asia/Taipei'; 6 7 // 從「年月日」建立 8 Carbon::createFromDate($year, $month, $day, $timezone); 9 10 // 從「時分秒」建立 11 Carbon::createFromTime($hour, $minute, $second, $timezone); 12 13 // 從完整的「年月日時分秒」建立 14 Carbon::create($year, $month, $day, $hour, $minute, $second, $timezone); 15 16 // 從指定的格式建立 17 Carbon::createFromFormat($format, $time, $tz); 18 echo Carbon::createFromFormat('Y-m-d H', '1975-05-21 22')->toDateTimeString(); // 1975-05-21 22:00:00 19 20 // 從時間戳記建立 21 echo Carbon::createFromTimeStamp(-1)->toDateTimeString(); // 1969-12-31 18:59:59 22 echo Carbon::createFromTimeStamp(-1, 'Europe/London')->toDateTimeString(); // 1970-01-01 00:59:59 23 echo Carbon::createFromTimeStampUTC(-1)->toDateTimeString(); // 1969-12-31 23:59:59
轉換日期
1 <?php 2 3 use Carbon\Carbon; 4 5 // 透過文字移動日期 6 $knownDate = Carbon::create(2001, 5, 21, 12); // create testing date 7 Carbon::setTestNow($knownDate); // set the mock 8 echo new Carbon('tomorrow'); // 2001-05-22 00:00:00 ... notice the time ! 9 echo new Carbon('yesterday'); // 2001-05-20 00:00:00 10 echo new Carbon('next wednesday'); // 2001-05-23 00:00:00 11 echo new Carbon('last friday'); // 2001-05-18 00:00:00 12 echo new Carbon('this thursday'); // 2001-05-24 00:00:00
取得日期資料
1 <?php 2 3 use Carbon\Carbon; 4 5 $dt = Carbon::parse('2012-9-5 23:26:11.123789'); 6 7 // 取的指定時間資料的資訊(整數) 8 var_dump($dt->year); // int(2012) 9 var_dump($dt->month); // int(9) 10 var_dump($dt->day); // int(5) 11 var_dump($dt->hour); // int(23) 12 var_dump($dt->minute); // int(26) 13 var_dump($dt->second); // int(11) 14 var_dump($dt->micro); // int(123789) 15 var_dump($dt->dayOfWeek); // int(3) 16 var_dump($dt->dayOfYear); // int(248) 17 var_dump($dt->weekOfMonth); // int(1) 18 var_dump($dt->weekOfYear); // int(36) 19 var_dump($dt->daysInMonth); // int(30) 20 var_dump($dt->timestamp); // int(1346901971) 21 var_dump(Carbon::createFromDate(1975, 5, 21)->age); // int(39) calculated vs now in the same tz 22 var_dump($dt->quarter); // int(3) 23 24 25 // 回傳與 UTC 差異的秒數 26 var_dump(Carbon::createFromTimestampUTC(0)->offset); // int(0) 27 var_dump(Carbon::createFromTimestamp(0)->offset); // int(-18000) 28 29 // 回傳與 UTC 差異的時數 30 var_dump(Carbon::createFromTimestamp(0)->offsetHours); // int(-5) 31 32 // 找出當天日否有日光節約時間 33 var_dump(Carbon::createFromDate(2012, 1, 1)->dst); // bool(false) 34 var_dump(Carbon::createFromDate(2012, 9, 1)->dst); // bool(true) 35 36 // 判斷指定的的時區是否與預設的時區相同 37 var_dump(Carbon::now()->local); // bool(true) 38 var_dump(Carbon::now('America/Vancouver')->local); // bool(false) 39 40 // 判斷是否為 UTC 的時區時間 41 var_dump(Carbon::now()->utc); // bool(false) 42 var_dump(Carbon::now('Europe/London')->utc); // bool(true) 43 var_dump(Carbon::createFromTimestampUTC(0)->utc); // bool(true) 44 45 // 取得時區實例 46 echo get_class(Carbon::now()->timezone); // DateTimeZone 47 echo get_class(Carbon::now()->tz); // DateTimeZone 48 49 // 取得時區實例的名稱 50 echo Carbon::now()->timezoneName; // America/Toronto 51 echo Carbon::now()->tzName; // America/Toronto
設定時間資料
1 <?php 2 3 use Carbon\Carbon; 4 5 $dt = Carbon::now(); 6 7 $dt->year = 1975; 8 $dt->month = 13; // 年份會強制 +1,且月份變為 1 月 9 $dt->month = 5; 10 $dt->day = 21; 11 $dt->hour = 22; 12 $dt->minute = 32; 13 $dt->second = 5; 14 15 $dt->timestamp = 169957925; // 這個設定不會變更時區 16 17 // 透過字串或是 DateTimeZone 實例去設定時區 18 $dt->timezone = new DateTimeZone('Europe/London'); 19 $dt->timezone = 'Europe/London'; 20 $dt->tz = 'Europe/London'; 21 22 23 24 // 鏈結設定方式 25 $dt->year(1975)->month(5)->day(21)->hour(22)->minute(32)->second(5)->toDateTimeString(); 26 $dt->setDate(1975, 5, 21)->setTime(22, 32, 5)->toDateTimeString(); 27 $dt->setDateTime(1975, 5, 21, 22, 32, 5)->toDateTimeString(); 28 29 $dt->timestamp(169957925)->timezone('Europe/London'); 30 31 $dt->tz('America/Toronto')->setTimezone('America/Vancouver');
格式化時間資料
1 <?php 2 3 use Carbon\Carbon; 4 5 $dt = Carbon::create(1975, 12, 25, 14, 15, 16); 6 7 var_dump($dt->toDateTimeString() == $dt); // bool(true) => uses __toString() 8 echo $dt->toDateString(); // 1975-12-25 9 echo $dt->toFormattedDateString(); // Dec 25, 1975 10 echo $dt->toTimeString(); // 14:15:16 11 echo $dt->toDateTimeString(); // 1975-12-25 14:15:16 12 echo $dt->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM 13 14 // 仍可以使用 format() 函式 15 echo $dt->format('l jS \\of F Y h:i:s A'); // Thursday 25th of December 1975 02:15:16 PM 16 17 // 常用的時間格式 18 echo $dt->toAtomString(); // in 1 Jahr 19 echo $dt->toCookieString(); // Thursday, 25-Dec-1975 14:15:16 EST 20 echo $dt->toIso8601String(); // 1975-12-25T14:15:16-0500 21 echo $dt->toRfc822String(); // Thu, 25 Dec 75 14:15:16 -0500 22 echo $dt->toRfc850String(); // Thursday, 25-Dec-75 14:15:16 EST 23 echo $dt->toRfc1036String(); // Thu, 25 Dec 75 14:15:16 -0500 24 echo $dt->toRfc1123String(); // Thu, 25 Dec 1975 14:15:16 -0500 25 echo $dt->toRfc2822String(); // Thu, 25 Dec 1975 14:15:16 -0500 26 echo $dt->toRfc3339String(); // 1975-12-25T14:15:16-05:00 27 echo $dt->toRssString(); // Thu, 25 Dec 1975 14:15:16 -0500 28 echo $dt->toW3cString(); // 1975-12-25T14:15:16-05:00
比較時間差異
1 <?php 2 3 use Carbon\Carbon; 4 5 echo Carbon::now()->tzName; // America/Toronto 6 $first = Carbon::create(2012, 9, 5, 23, 26, 11); 7 $second = Carbon::create(2012, 9, 5, 20, 26, 11, 'America/Vancouver'); 8 9 echo $first->toDateTimeString(); // 2012-09-05 23:26:11 10 echo $first->tzName; // America/Toronto 11 echo $second->toDateTimeString(); // 2012-09-05 20:26:11 12 echo $second->tzName; // America/Vancouver 13 14 // 大於、等於、小於 15 var_dump($first->eq($second)); // bool(true) 16 var_dump($first->ne($second)); // bool(false) 17 var_dump($first->gt($second)); // bool(false) 18 var_dump($first->gte($second)); // bool(true) 19 var_dump($first->lt($second)); // bool(false) 20 var_dump($first->lte($second)); // bool(true) 21 22 $first->setDateTime(2012, 1, 1, 0, 0, 0); 23 $second->setDateTime(2012, 1, 1, 0, 0, 0); // Remember tz is 'America/Vancouver' 24 25 var_dump($first->eq($second)); // bool(false) 26 var_dump($first->ne($second)); // bool(true) 27 var_dump($first->gt($second)); // bool(false) 28 var_dump($first->gte($second)); // bool(false) 29 var_dump($first->lt($second)); // bool(true) 30 var_dump($first->lte($second)); // bool(true) 31 32 33 34 // 時間區間比較 35 $first = Carbon::create(2012, 9, 5, 1); 36 $second = Carbon::create(2012, 9, 5, 5); 37 var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second)); // bool(true) 38 var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second)); // bool(true) 39 var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second, false)); // bool(false) 40 41 42 // 時間大小比較 43 $dt1 = Carbon::create(2012, 1, 1, 0, 0, 0); 44 $dt2 = Carbon::create(2014, 1, 30, 0, 0, 0); 45 echo $dt1->min($dt2); // 2012-01-01 00:00:00 46 47 $dt1 = Carbon::create(2012, 1, 1, 0, 0, 0); 48 $dt2 = Carbon::create(2014, 1, 30, 0, 0, 0); 49 echo $dt1->max($dt2); // 2014-01-30 00:00:00 50 51 // now is the default param 52 $dt1 = Carbon::create(2000, 1, 1, 0, 0, 0); 53 echo $dt1->max(); 54 55 56 // 時間差異運算 57 echo Carbon::now('America/Vancouver')->diffInSeconds(Carbon::now('Europe/London')); // 0 58 59 $dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto'); 60 $dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver'); 61 echo $dtOttawa->diffInHours($dtVancouver); // 3 62 63 echo $dtOttawa->diffInHours($dtVancouver, false); // 3 64 echo $dtVancouver->diffInHours($dtOttawa, false); // -3 65 66 $dt = Carbon::create(2012, 1, 31, 0); 67 echo $dt->diffInDays($dt->copy()->addMonth()); // 31 68 echo $dt->diffInDays($dt->copy()->subMonth(), false); // -31 69 70 $dt = Carbon::create(2012, 4, 30, 0); 71 echo $dt->diffInDays($dt->copy()->addMonth()); // 30 72 echo $dt->diffInDays($dt->copy()->addWeek()); // 7 73 74 $dt = Carbon::create(2012, 1, 1, 0); 75 echo $dt->diffInMinutes($dt->copy()->addSeconds(59)); // 0 76 echo $dt->diffInMinutes($dt->copy()->addSeconds(60)); // 1 77 echo $dt->diffInMinutes($dt->copy()->addSeconds(119)); // 1 78 echo $dt->diffInMinutes($dt->copy()->addSeconds(120)); // 2 79 80 echo $dt->addSeconds(120)->secondsSinceMidnight(); // 120
時間狀態
1 <?php 2 3 use Carbon\Carbon; 4 5 $dt = Carbon::now(); 6 7 $dt->isWeekday(); 8 $dt->isWeekend(); 9 $dt->isYesterday(); 10 $dt->isToday(); 11 $dt->isTomorrow(); 12 $dt->isFuture(); 13 $dt->isPast(); 14 $dt->isLeapYear(); 15 $dt->isSameDay(Carbon::now()); 16 $born = Carbon::createFromDate(1987, 4, 23); 17 $noCake = Carbon::createFromDate(2014, 9, 26); 18 $yesCake = Carbon::createFromDate(2014, 4, 23); 19 var_dump($born->isBirthday($noCake)); // bool(false) 20 var_dump($born->isBirthday($yesCake)); // bool(true)
時間運算
1 <?php 2 3 use Carbon\Carbon; 4 5 $dt = Carbon::create(2012, 1, 31, 0); 6 7 echo $dt->toDateTimeString(); // 2012-01-31 00:00:00 8 9 echo $dt->addYears(5); // 2017-01-31 00:00:00 10 echo $dt->addYear(); // 2018-01-31 00:00:00 11 echo $dt->subYear(); // 2017-01-31 00:00:00 12 echo $dt->subYears(5); // 2012-01-31 00:00:00 13 14 echo $dt->addMonths(60); // 2017-01-31 00:00:00 15 echo $dt->addMonth(); // 2017-03-03 00:00:00 equivalent of $dt->month($dt->month + 1); so it wraps 16 echo $dt->subMonth(); // 2017-02-03 00:00:00 17 echo $dt->subMonths(60); // 2012-02-03 00:00:00 18 19 echo $dt->addDays(29); // 2012-03-03 00:00:00 20 echo $dt->addDay(); // 2012-03-04 00:00:00 21 echo $dt->subDay(); // 2012-03-03 00:00:00 22 echo $dt->subDays(29); // 2012-02-03 00:00:00 23 24 echo $dt->addWeekdays(4); // 2012-02-09 00:00:00 25 echo $dt->addWeekday(); // 2012-02-10 00:00:00 26 echo $dt->subWeekday(); // 2012-02-09 00:00:00 27 echo $dt->subWeekdays(4); // 2012-02-03 00:00:00 28 29 echo $dt->addWeeks(3); // 2012-02-24 00:00:00 30 echo $dt->addWeek(); // 2012-03-02 00:00:00 31 echo $dt->subWeek(); // 2012-02-24 00:00:00 32 echo $dt->subWeeks(3); // 2012-02-03 00:00:00 33 34 echo $dt->addHours(24); // 2012-02-04 00:00:00 35 echo $dt->addHour(); // 2012-02-04 01:00:00 36 echo $dt->subHour(); // 2012-02-04 00:00:00 37 echo $dt->subHours(24); // 2012-02-03 00:00:00 38 39 echo $dt->addMinutes(61); // 2012-02-03 01:01:00 40 echo $dt->addMinute(); // 2012-02-03 01:02:00 41 echo $dt->subMinute(); // 2012-02-03 01:01:00 42 echo $dt->subMinutes(61); // 2012-02-03 00:00:00 43 44 echo $dt->addSeconds(61); // 2012-02-03 00:01:01 45 echo $dt->addSecond(); // 2012-02-03 00:01:02 46 echo $dt->subSecond(); // 2012-02-03 00:01:01 47 echo $dt->subSeconds(61); // 2012-02-03 00:00:00
人類閱讀時間格式
1 <?php 2 3 use Carbon\Carbon; 4 5 // 通常會用在留言的時間顯示 6 // 該時間會比較與現在的時間的差異 7 echo Carbon::now()->subDays(5)->diffForHumans(); // 5 days ago 8 9 echo Carbon::now()->diffForHumans(Carbon::now()->subYear()); // 1 year after 10 11 $dt = Carbon::createFromDate(2011, 8, 1); 12 13 echo $dt->diffForHumans($dt->copy()->addMonth()); // 1 month before 14 echo $dt->diffForHumans($dt->copy()->subMonth()); // 1 month after 15 16 echo Carbon::now()->addSeconds(5)->diffForHumans(); // 5 seconds from now 17 18 echo Carbon::now()->subDays(24)->diffForHumans(); // 3 weeks ago 19 echo Carbon::now()->subDays(24)->diffForHumans(null, true); // 3 weeks
時間常數
1 <?php 2 3 use Carbon\Carbon; 4 5 var_dump(Carbon::SUNDAY); // int(0) 6 var_dump(Carbon::MONDAY); // int(1) 7 var_dump(Carbon::TUESDAY); // int(2) 8 var_dump(Carbon::WEDNESDAY); // int(3) 9 var_dump(Carbon::THURSDAY); // int(4) 10 var_dump(Carbon::FRIDAY); // int(5) 11 var_dump(Carbon::SATURDAY); // int(6) 12 13 var_dump(Carbon::YEARS_PER_CENTURY); // int(100) 14 var_dump(Carbon::YEARS_PER_DECADE); // int(10) 15 var_dump(Carbon::MONTHS_PER_YEAR); // int(12) 16 var_dump(Carbon::WEEKS_PER_YEAR); // int(52) 17 var_dump(Carbon::DAYS_PER_WEEK); // int(7) 18 var_dump(Carbon::HOURS_PER_DAY); // int(24) 19 var_dump(Carbon::MINUTES_PER_HOUR); // int(60) 20 var_dump(Carbon::SECONDS_PER_MINUTE); // int(60)
參考資料
纵然世间炎凉百态!我自依旧初心不改!!