php把年份转为int,PHP将DateInterval转换为int
我正在使用此代碼:
$due_date = new DateTime($_POST['due_date']);
$today = new DateTime();
$months = $due_date->diff($today);
$months->format("%m");
$fine = 0.02 * $price * $months; // i got error in this line
$bill = $price + $fine;
我想計算一下,如果有人遲到,那么他們將每月被罰款.錯誤消息是:
Object of class DateInterval could not be converted to int
解決方法:
出現錯誤消息是因為$months不是一個int,而是一個類似這樣的Datetime對象:
DateInterval Object
(
[y] => 0
[m] => 4
[d] => 12
[h] => 6
[i] => 56
[s] => 9
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 133
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
你可以這樣獲得月份的整數值
$due_date = new DateTime('13-02-2016');
$today = new DateTime();
$months = $due_date->diff($today);
echo $months->m;
所以基本上你的代碼看起來像
$due_date = new DateTime($_POST['due_date']);
$today = new DateTime();
$months = $due_date->diff($today);
$fine = 0.02 * $price * $months->m; // i got no error in this line
$bill = $price + $fine;
標簽:date-math,dateinterval,datetime,php,date
來源: https://codeday.me/bug/20191118/2028532.html
總結
以上是生活随笔為你收集整理的php把年份转为int,PHP将DateInterval转换为int的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab龙格库塔法求通解,基于mat
- 下一篇: php ajax邮箱,PHP+Ajax异