Laravel Carbon 安裝 composer require nesbot/carbon
如何使用 可以在php artisan tinker
裡測試Carbon\Carbon::now()
以下是我個人在工作上常使用的範例
use Carbon \Carbon ; $now = Carbon::now(); $formatedDate = $now ->format('Y-m-d H:i:s' );$nowTime = $now ->toTimeString(); $nowDay = $now ->dayOfWeek $nowDate = $now ->toDateString(); $nowDateTime = $now ->toDateTimeString(); $nowTimestamp = $now ->timestamp;
官方範例程式碼 <?php use Carbon \Carbon ;printf("Right now is %s" , Carbon::now()->toDateTimeString()); printf("Right now in Vancouver is %s" , Carbon::now('America/Vancouver' )); $tomorrow = Carbon::now()->addDay();$lastWeek = Carbon::now()->subWeek();$nextSummerOlympics = Carbon::createFromDate(2016 )->addYears(4 );$officialDate = Carbon::now()->toRfc2822String();$howOldAmI = Carbon::createFromDate(1975 , 5 , 21 )->age;$noonTodayLondonTime = Carbon::createFromTime(12 , 0 , 0 , 'Europe/London' );$internetWillBlowUpOn = Carbon::create(2038 , 01 , 19 , 3 , 14 , 7 , 'GMT' );Carbon::setTestNow(Carbon::createFromDate(2000 , 1 , 1 )); if (Carbon::now()->gte($internetWillBlowUpOn )) { die (); } Carbon::setTestNow(); if (Carbon::now()->isWeekend()) { echo 'Party!' ; } echo Carbon::now()->subMinutes(2 )->diffForHumans(); echo Carbon::now()->subMinutes(2 )->locale('zh_CN' )->diffForHumans(); echo Carbon::parse('2019-07-23 14:51' )->isoFormat('LLLL' ); echo Carbon::parse('2019-07-23 14:51' )->locale('fr_FR' )->isoFormat('LLLL' ); $daysSinceEpoch = Carbon::createFromTimestamp(0 )->diffInDays();
更多用法請見官方網站