1 回答

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超2個(gè)贊
您的日期格式為歐洲格式 (DD/MM/YYYY),這意味著您需要使用DateTime::createFromFormat()
指定正確的格式才能DateTime
正確處理它。這是由于 P?HP 在看到 NN/NN/NNNN 日期格式時(shí)假設(shè)美國日期格式。
<?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 關(guān)注
- 186 瀏覽
添加回答
舉報(bào)