carbon 計算當前時間與給定時間差

cainiao9980發表於2017-11-09

$date = '2017-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);
等等,可計算當前時間與給定的時間差。
某些時候,如果計算超過截至日期等等時候比較好用。

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章