laravel Carbon函数

2018-11-15 13:55:29

参考资料:laravel Carbon函数

快速切換前後日期

<?phpuse Carbon\Carbon;$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

建立特定日期的時間

<?phpuse Carbon\Carbon;$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:59echo Carbon::createFromTimeStamp(-1, 'Europe/London')->toDateTimeString();       // 1970-01-01 00:59:59echo Carbon::createFromTimeStampUTC(-1)->toDateTimeString();                     // 1969-12-31 23:59:59

轉換日期

<?phpuse Carbon\Carbon;// 透過文字移動日期$knownDate = Carbon::create(2001, 5, 21, 12);          // create testing dateCarbon::setTestNow($knownDate);                        // set the mockecho new Carbon('tomorrow');                           // 2001-05-22 00:00:00  ... notice the time !echo new Carbon('yesterday');                          // 2001-05-20 00:00:00echo new Carbon('next wednesday');                     // 2001-05-23 00:00:00echo new Carbon('last friday');                        // 2001-05-18 00:00:00echo new Carbon('this thursday');                      // 2001-05-24 00:00:00

取得日期資料

<?phpuse Carbon\Carbon;$dt = Carbon::parse('2012-9-5 23:26:11.123789');// 取的指定時間資料的資訊(整數)var_dump($dt->year);                                         // int(2012)var_dump($dt->month);                                        // int(9)var_dump($dt->day);                                          // int(5)var_dump($dt->hour);                                         // int(23)var_dump($dt->minute);                                       // int(26)var_dump($dt->second);                                       // int(11)var_dump($dt->micro);                                        // int(123789)var_dump($dt->dayOfWeek);                                    // int(3)var_dump($dt->dayOfYear);                                    // int(248)var_dump($dt->weekOfMonth);                                  // int(1)var_dump($dt->weekOfYear);                                   // int(36)var_dump($dt->daysInMonth);                                  // int(30)var_dump($dt->timestamp);                                    // int(1346901971)var_dump(Carbon::createFromDate(1975, 5, 21)->age);          // int(39) calculated vs now in the same tzvar_dump($dt->quarter);                                      // int(3)// 回傳與 UTC 差異的秒數var_dump(Carbon::createFromTimestampUTC(0)->offset);         // int(0)var_dump(Carbon::createFromTimestamp(0)->offset);            // int(-18000)// 回傳與 UTC 差異的時數var_dump(Carbon::createFromTimestamp(0)->offsetHours);       // int(-5)// 找出當天日否有日光節約時間var_dump(Carbon::createFromDate(2012, 1, 1)->dst);           // bool(false)var_dump(Carbon::createFromDate(2012, 9, 1)->dst);           // bool(true)// 判斷指定的的時區是否與預設的時區相同var_dump(Carbon::now()->local);                              // bool(true)var_dump(Carbon::now('America/Vancouver')->local);           // bool(false)// 判斷是否為 UTC 的時區時間var_dump(Carbon::now()->utc);                                // bool(false)var_dump(Carbon::now('Europe/London')->utc);                 // bool(true)var_dump(Carbon::createFromTimestampUTC(0)->utc);            // bool(true)// 取得時區實例echo get_class(Carbon::now()->timezone);                     // DateTimeZoneecho get_class(Carbon::now()->tz);                           // DateTimeZone// 取得時區實例的名稱echo Carbon::now()->timezoneName;                            // America/Torontoecho Carbon::now()->tzName;                                  // America/Toronto

設定時間資料

<?phpuse Carbon\Carbon;$dt = Carbon::now();$dt->year = 1975;$dt->month = 13;             // 年份會強制 +1,且月份變為 1 月$dt->month = 5;$dt->day = 21;$dt->hour = 22;$dt->minute = 32;$dt->second = 5;$dt->timestamp = 169957925;  // 這個設定不會變更時區// 透過字串或是 DateTimeZone 實例去設定時區$dt->timezone = new DateTimeZone('Europe/London');$dt->timezone = 'Europe/London';$dt->tz = 'Europe/London';// 鏈結設定方式$dt->year(1975)->month(5)->day(21)->hour(22)->minute(32)->second(5)->toDateTimeString();$dt->setDate(1975, 5, 21)->setTime(22, 32, 5)->toDateTimeString();$dt->setDateTime(1975, 5, 21, 22, 32, 5)->toDateTimeString();$dt->timestamp(169957925)->timezone('Europe/London');$dt->tz('America/Toronto')->setTimezone('America/Vancouver');

格式化時間資料

<?phpuse Carbon\Carbon;$dt = Carbon::create(1975, 12, 25, 14, 15, 16);var_dump($dt->toDateTimeString() == $dt);          // bool(true) => uses __toString()echo $dt->toDateString();                          // 1975-12-25echo $dt->toFormattedDateString();                 // Dec 25, 1975echo $dt->toTimeString();                          // 14:15:16echo $dt->toDateTimeString();                      // 1975-12-25 14:15:16echo $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 Jahrecho $dt->toCookieString();    // Thursday, 25-Dec-1975 14:15:16 ESTecho $dt->toIso8601String();   // 1975-12-25T14:15:16-0500echo $dt->toRfc822String();    // Thu, 25 Dec 75 14:15:16 -0500echo $dt->toRfc850String();    // Thursday, 25-Dec-75 14:15:16 ESTecho $dt->toRfc1036String();   // Thu, 25 Dec 75 14:15:16 -0500echo $dt->toRfc1123String();   // Thu, 25 Dec 1975 14:15:16 -0500echo $dt->toRfc2822String();   // Thu, 25 Dec 1975 14:15:16 -0500echo $dt->toRfc3339String();   // 1975-12-25T14:15:16-05:00echo $dt->toRssString();       // Thu, 25 Dec 1975 14:15:16 -0500echo $dt->toW3cString();       // 1975-12-25T14:15:16-05:00

比較時間差異

<?phpuse Carbon\Carbon;echo Carbon::now()->tzName;                        // America/Toronto$first = Carbon::create(2012, 9, 5, 23, 26, 11);$second = Carbon::create(2012, 9, 5, 20, 26, 11, 'America/Vancouver');echo $first->toDateTimeString();                   // 2012-09-05 23:26:11echo $first->tzName;                               // America/Torontoecho $second->toDateTimeString();                  // 2012-09-05 20:26:11echo $second->tzName;                              // America/Vancouver// 大於、等於、小於var_dump($first->eq($second));                     // bool(true)var_dump($first->ne($second));                     // bool(false)var_dump($first->gt($second));                     // bool(false)var_dump($first->gte($second));                    // bool(true)var_dump($first->lt($second));                     // bool(false)var_dump($first->lte($second));                    // bool(true)$first->setDateTime(2012, 1, 1, 0, 0, 0);$second->setDateTime(2012, 1, 1, 0, 0, 0);         // Remember tz is 'America/Vancouver'var_dump($first->eq($second));                     // bool(false)var_dump($first->ne($second));                     // bool(true)var_dump($first->gt($second));                     // bool(false)var_dump($first->gte($second));                    // bool(false)var_dump($first->lt($second));                     // bool(true)var_dump($first->lte($second));                    // bool(true)// 時間區間比較$first = Carbon::create(2012, 9, 5, 1);$second = Carbon::create(2012, 9, 5, 5);var_dump(Carbon::create(2012, 9, 5, 3)->between($first, $second));          // bool(true)var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second));          // bool(true)var_dump(Carbon::create(2012, 9, 5, 5)->between($first, $second, false));   // bool(false)// 時間大小比較$dt1 = Carbon::create(2012, 1, 1, 0, 0, 0);$dt2 = Carbon::create(2014, 1, 30, 0, 0, 0);echo $dt1->min($dt2);                              // 2012-01-01 00:00:00$dt1 = Carbon::create(2012, 1, 1, 0, 0, 0);$dt2 = Carbon::create(2014, 1, 30, 0, 0, 0);echo $dt1->max($dt2);                              // 2014-01-30 00:00:00// now is the default param$dt1 = Carbon::create(2000, 1, 1, 0, 0, 0);echo $dt1->max();// 時間差異運算echo Carbon::now('America/Vancouver')->diffInSeconds(Carbon::now('Europe/London')); // 0$dtOttawa = Carbon::createFromDate(2000, 1, 1, 'America/Toronto');$dtVancouver = Carbon::createFromDate(2000, 1, 1, 'America/Vancouver');echo $dtOttawa->diffInHours($dtVancouver);                             // 3echo $dtOttawa->diffInHours($dtVancouver, false);                      // 3echo $dtVancouver->diffInHours($dtOttawa, false);                      // -3$dt = Carbon::create(2012, 1, 31, 0);echo $dt->diffInDays($dt->copy()->addMonth());                         // 31echo $dt->diffInDays($dt->copy()->subMonth(), false);                  // -31$dt = Carbon::create(2012, 4, 30, 0);echo $dt->diffInDays($dt->copy()->addMonth());                         // 30echo $dt->diffInDays($dt->copy()->addWeek());                          // 7$dt = Carbon::create(2012, 1, 1, 0);echo $dt->diffInMinutes($dt->copy()->addSeconds(59));                  // 0echo $dt->diffInMinutes($dt->copy()->addSeconds(60));                  // 1echo $dt->diffInMinutes($dt->copy()->addSeconds(119));                 // 1echo $dt->diffInMinutes($dt->copy()->addSeconds(120));                 // 2echo $dt->addSeconds(120)->secondsSinceMidnight();                     // 120

時間狀態

<?phpuse Carbon\Carbon;$dt = Carbon::now();$dt->isWeekday();$dt->isWeekend();$dt->isYesterday();$dt->isToday();$dt->isTomorrow();$dt->isFuture();$dt->isPast();$dt->isLeapYear();$dt->isSameDay(Carbon::now());$born = Carbon::createFromDate(1987, 4, 23);$noCake = Carbon::createFromDate(2014, 9, 26);$yesCake = Carbon::createFromDate(2014, 4, 23);var_dump($born->isBirthday($noCake));              // bool(false)  var_dump($born->isBirthday($yesCake));             // bool(true)

時間運算

<?phpuse Carbon\Carbon;$dt = Carbon::create(2012, 1, 31, 0);echo $dt->toDateTimeString();            // 2012-01-31 00:00:00echo $dt->addYears(5);                   // 2017-01-31 00:00:00echo $dt->addYear();                     // 2018-01-31 00:00:00echo $dt->subYear();                     // 2017-01-31 00:00:00echo $dt->subYears(5);                   // 2012-01-31 00:00:00echo $dt->addMonths(60);                 // 2017-01-31 00:00:00echo $dt->addMonth();                    // 2017-03-03 00:00:00 equivalent of $dt->month($dt->month + 1); so it wrapsecho $dt->subMonth();                    // 2017-02-03 00:00:00echo $dt->subMonths(60);                 // 2012-02-03 00:00:00echo $dt->addDays(29);                   // 2012-03-03 00:00:00echo $dt->addDay();                      // 2012-03-04 00:00:00echo $dt->subDay();                      // 2012-03-03 00:00:00echo $dt->subDays(29);                   // 2012-02-03 00:00:00echo $dt->addWeekdays(4);                // 2012-02-09 00:00:00echo $dt->addWeekday();                  // 2012-02-10 00:00:00echo $dt->subWeekday();                  // 2012-02-09 00:00:00echo $dt->subWeekdays(4);                // 2012-02-03 00:00:00echo $dt->addWeeks(3);                   // 2012-02-24 00:00:00echo $dt->addWeek();                     // 2012-03-02 00:00:00echo $dt->subWeek();                     // 2012-02-24 00:00:00echo $dt->subWeeks(3);                   // 2012-02-03 00:00:00echo $dt->addHours(24);                  // 2012-02-04 00:00:00echo $dt->addHour();                     // 2012-02-04 01:00:00echo $dt->subHour();                     // 2012-02-04 00:00:00echo $dt->subHours(24);                  // 2012-02-03 00:00:00echo $dt->addMinutes(61);                // 2012-02-03 01:01:00echo $dt->addMinute();                   // 2012-02-03 01:02:00echo $dt->subMinute();                   // 2012-02-03 01:01:00echo $dt->subMinutes(61);                // 2012-02-03 00:00:00echo $dt->addSeconds(61);                // 2012-02-03 00:01:01echo $dt->addSecond();                   // 2012-02-03 00:01:02echo $dt->subSecond();                   // 2012-02-03 00:01:01echo $dt->subSeconds(61);                // 2012-02-03 00:00:00

人類閱讀時間格式

<?phpuse Carbon\Carbon;// 通常會用在留言的時間顯示// 該時間會比較與現在的時間的差異echo Carbon::now()->subDays(5)->diffForHumans();               // 5 days agoecho Carbon::now()->diffForHumans(Carbon::now()->subYear());   // 1 year after$dt = Carbon::createFromDate(2011, 8, 1);echo $dt->diffForHumans($dt->copy()->addMonth());              // 1 month beforeecho $dt->diffForHumans($dt->copy()->subMonth());              // 1 month afterecho Carbon::now()->addSeconds(5)->diffForHumans();            // 5 seconds from nowecho Carbon::now()->subDays(24)->diffForHumans();              // 3 weeks agoecho Carbon::now()->subDays(24)->diffForHumans(null, true);    // 3 weeks

時間常數

<?phpuse Carbon\Carbon;var_dump(Carbon::SUNDAY);                          // int(0)var_dump(Carbon::MONDAY);                          // int(1)var_dump(Carbon::TUESDAY);                         // int(2)var_dump(Carbon::WEDNESDAY);                       // int(3)var_dump(Carbon::THURSDAY);                        // int(4)var_dump(Carbon::FRIDAY);                          // int(5)var_dump(Carbon::SATURDAY);                        // int(6)var_dump(Carbon::YEARS_PER_CENTURY);               // int(100)var_dump(Carbon::YEARS_PER_DECADE);                // int(10)var_dump(Carbon::MONTHS_PER_YEAR);                 // int(12)var_dump(Carbon::WEEKS_PER_YEAR);                  // int(52)var_dump(Carbon::DAYS_PER_WEEK);                   // int(7)var_dump(Carbon::HOURS_PER_DAY);                   // int(24)var_dump(Carbon::MINUTES_PER_HOUR);                // int(60)var_dump(Carbon::SECONDS_PER_MINUTE);              // int(60)

參考資料


  • 2019-08-22 13:35:27

    Generator函数的语法

    执行Generator函数会返回一个遍历器对象,也就是说,Generator函数除了是状态机还是一个遍历器对象生成函数。 返回遍历器对象,可以依次遍历Generator函数内部的每一个状态。

  • 2019-08-22 16:38:15

    理解JS原型对象与原型链(重要清晰)

    JavaScript 常被描述为一种基于原型的语言 (prototype-based language)——每个对象对应拥有一个原型,对象以其原型为模板、从原型继承方法和属性。而同时原型也是对象,它也拥有原型,并从中继承方法和属性,一层一层、以此类推。这种关系常被称为原型链 (prototype chain),它解释了为何一个对象会拥有定义在其他对象中的属性和方法。

  • 2019-08-22 17:26:21

    详解javaScript的深拷贝

    最开始意识到深拷贝的重要性是在我使用redux的时候(react + redux), redux的机制要求在reducer中必须返回一个新的对象,而不能对原来的对象做改动,事实上,当时我当然不会主动犯这个错误,但很多时候,一不小心可能就会修改了原来的对象,例如:var newObj = obj; newObj.xxx = xxx 实际上,这个时候newObj和obj两个引用指向的是同一个对象,我修改了newObj,实际上也就等同于修改了obj,这,就是我和深浅拷贝的第一次相遇。

  • 2019-08-22 19:14:21

    Android Studio 3.5最新特性

    Android Studio(以下简称为AS) 3.5正式版终于发布了,从第一个bate版本发布到正式版本,历时三个半月。AS一直以来被开发者吐槽,因此谷歌也放慢了版本的变化,对测试版本进行大力度的优化,提高了稳定性。从3.3版本开始,谷歌启动了名为Project Marble的计划,意为谷歌团队致力于使集成开发环境(IDE)的基本功能和流程变得坚如磐石,同时精炼和完善面向用户的功能。而AS 3.5则是Project Marble主要成果的版本,下面来介绍主要成果。

  • 2019-08-27 05:43:13

    Laravel 门面自动补全工具 laravel-ide-helper

    当我们在 PhpStorm 编辑器中,开发 Laravel 框架的项目时,很多类方法都不能自动补全和定位,比如 Facade 门面的方法,DB::table()、Route::get() 等。