亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

DateTime 解析時間字符串失敗

DateTime 解析時間字符串失敗

PHP
嗶嗶one 2023-04-15 17:54:24
我在從 JSON 帶來的數組中為每個項目傳遞 2 個日期和時間字符串。這些日期已成功存儲在數組中,但DateTime由于某種原因函數不喜歡它們。我試過使用不同的格式,只是日期,只是時間,但沒有任何效果。我提供了我正在使用的 JSON 文件和我的 PHP 測試文件。<?php     error_reporting(E_ALL);    ini_set('display_errors', 1);    $revokes = jsonDecode(file_get_contents("../revokes.json"), true);    $certificates = $revokes['certificates'];    // Prints the revokes array    // print_r($revokes);    $dates = array();    foreach ($certificates as $certificate_key => $certificate) {        $signed = $certificate['signed'];        $revoked = $certificate['revoked'];        $dates[] = array(            "signed" => $signed,            "revoked" => $revoked        );    }    // Prints the dates    // print_r($dates);    $intervals = array();    foreach ($dates as $key) {        $newTimeAdd = new DateTime($key["signed"]);        $newTimeRead = new DateTime($key["revoked"]);        $interval = $newTimeAdd->diff($newTimeRead);        // returns 0 on all elements of the interval array.        // var_dump($interval);        $intervals[] = $interval->days;//get days    }    if(!empty($intervals)) {        $average = average($intervals);    }    // Prints nothing    // print_r($intervals);     function average($arr) {        return array_sum($arr)/count($arr);    }PHP約會時間日期時間格式
查看完整描述

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


查看完整回答
反對 回復 2023-04-15
  • 1 回答
  • 0 關注
  • 188 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號