1 回答
TA贡献1784条经验 获得超2个赞
您的日期格式为欧洲格式 (DD/MM/YYYY),这意味着您需要使用DateTime::createFromFormat()
指定正确的格式才能DateTime
正确处理它。这是由于 P HP 在看到 NN/NN/NNNN 日期格式时假设美国日期格式。
<?php
$json = json_decode('{
"lifeExp": "2 Days",
"certificates": [
{
"name": "CCS Group Pte Ltd",
"signed": "22/05/2020 10:31:00",
"revoked": "23/05/2020 5:40:00",
"files": {
"p12": "certificates/:id/certificate.p12",
"pem": "certificates/:id/certificate.pem",
"key": "certificates/:id/certificate.key",
"password": "certificates/:id/certificate.password"
}
},
{
"name": "Hoola Inc",
"signed": "16/05/2020 12:40:00",
"revoked": "19/05/2020 04:00:00",
"files": {
"p12": "certificates/:id/certificate.p12",
"pem": "certificates/:id/certificate.pem",
"key": "certificates/:id/certificate.key",
"password": "certificates/:id/certificate.password"
}
}
]
}', true);
$signed = $json['certificates'][1]['signed'];
$revoked = $json['certificates'][1]['revoked'];
$newTimeAdd = DateTime::createFromFormat('d/m/Y H:i:s', $signed);
$newTimeRead = DateTime::createFromFormat('d/m/Y H:i:s', $revoked);
$interval = $newTimeAdd->diff($newTimeRead);
echo $interval->days;
输出
2
- 1 回答
- 0 关注
- 153 浏览
添加回答
举报