DateTime 解析时间字符串失败

问题描述 投票:0回答:1

我在数组中为每个项目传递2个日期和时间字符串,这是从JSON中带来的。

这些日期被成功地存储在数组中,但是在数组中的 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);
    }


    function jsonDecode($json, $assoc = false)
    {
        $ret = json_decode($json, $assoc);
        if ($error = json_last_error())
        {
            $errorReference = [
                JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded.',
                JSON_ERROR_STATE_MISMATCH => 'Invalid or malformed JSON.',
                JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded.',
                JSON_ERROR_SYNTAX => 'Syntax error.',
                JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded.',
                JSON_ERROR_RECURSION => 'One or more recursive references in the value to be encoded.',
                JSON_ERROR_INF_OR_NAN => 'One or more NAN or INF values in the value to be encoded.',
                JSON_ERROR_UNSUPPORTED_TYPE => 'A value of a type that cannot be encoded was given.',
            ];
            $errStr = isset($errorReference[$error]) ? $errorReference[$error] : "Unknown error ($error)";
            throw new \Exception("JSON decode error ($error): $errStr");
        }
        return $ret;
    }
?>
{
    "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"
            }
        }
    ]
}
php datetime datetime-format
1个回答
0
投票

你的日期格式是欧洲格式(DDMMYYYY),这意味着你需要使用 DateTime::createFromFormat() 指定正确的格式,以拥有 DateTime 正确处理。这是由于P.

<?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

演示

© www.soinside.com 2019 - 2024. All rights reserved.