PHP 日期時間類 Carbon 的常見用法


Carbon 繼承了 PHP DateTime 類,所以 DateTime 類的方法同樣適用於 Carbon 類。

初始化一個日期

use Carbon\Carbon; $today = new Carbon('2018-08-01'); 

本月第一天,上月第一天

$first_day = new Carbon('first day of this month'); $first_day = new Carbon('first day of last month'); return $month->format('Y-m'); // 2018-08 

今日日期

echo Carbon::today()->toDateString(); // 2018-09-10 

當前時間

Carbon::now(); // 2018-10-08 14:24:15.074542 Asia/Shanghai (+08:00) echo Carbon::now(); // 2018-10-08 14:28:10 

時間差

$start = new Carbon('2018-10-04 15:00:03'); $end = new Carbon('2018-10-05 17:00:09'); $start->diff($end)->format('%H:%I:%S'); 02:00:06 

參考

https://carbon.nesbot.com/docs/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM