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;
我想計(jì)算一下,如果有人遲到,那么他們將每月被罰款.錯(cuò)誤消息是:
Object of class DateInterval could not be converted to int
解決方法:
出現(xiàn)錯(cuò)誤消息是因?yàn)?months不是一個(gè)int,而是一個(gè)類似這樣的Datetime對(duì)象:
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
)
你可以這樣獲得月份的整數(shù)值
$due_date = new DateTime('13-02-2016');
$today = new DateTime();
$months = $due_date->diff($today);
echo $months->m;
所以基本上你的代碼看起來(lái)像
$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;
標(biāo)簽:date-math,dateinterval,datetime,php,date
來(lái)源: https://codeday.me/bug/20191118/2028532.html
總結(jié)
以上是生活随笔為你收集整理的php把年份转为int,PHP将DateInterval转换为int的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab龙格库塔法求通解,基于mat
- 下一篇: php ajax邮箱,PHP+Ajax异