第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

DateTime 解析時(shí)間字符串失敗

DateTime 解析時(shí)間字符串失敗

PHP
嗶嗶one 2023-04-15 17:54:24
我在從 JSON 帶來的數(shù)組中為每個(gè)項(xiàng)目傳遞 2 個(gè)日期和時(shí)間字符串。這些日期已成功存儲(chǔ)在數(shù)組中,但DateTime由于某種原因函數(shù)不喜歡它們。我試過使用不同的格式,只是日期,只是時(shí)間,但沒有任何效果。我提供了我正在使用的 JSON 文件和我的 PHP 測(cè)試文件。<?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約會(huì)時(shí)間日期時(shí)間格式
查看完整描述

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


查看完整回答
反對(duì) 回復(fù) 2023-04-15
  • 1 回答
  • 0 關(guān)注
  • 186 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)