項目中有計算當前時間與指定時間差的需求,記錄下實現代碼。
$date = '2020-11-11 12:59:59'; $carbon = carbon::parse ($date); // 格式化一個時間日期字符串為 carbon 對象 $int = (new Carbon)->diffInSeconds ($carbon, false); // $int 為正負數 or $int = (new Carbon)->diffInSeconds ($carbon, true); // $int 為正負數的絕對值 類似還有: $int = (new Carbon)->diffInMinutes($carbon, true); $int = (new Carbon)->diffInHours($carbon, true);
Enjoy it !