0

I am passing 2 date and time strings per item in the array which is brought over from JSON.

These dates are successfully stored in the array but the DateTime function doesn't like them for some reason.

I have tried using different formats, just the date, just the time but nothing worked.

I have provided the JSON file and my PHP Tests file I am using.

<?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"
            }
        }
    ]
}
2
  • What are the values for $key["signed"] and $key["revoked"]?
    – John Conde
    May 23, 2020 at 1:30
  • @JohnConde my bad i forgot to include the json file updating now! May 23, 2020 at 1:32

1 Answer 1

0

Your date formats are in European format (DD/MM/YYYY) which means you'll need to use DateTime::createFromFormat() to specify the correct format to have DateTime handle it correctly. This is due to PHP assuming US date format when it sees the NN/NN/NNNN date format.

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

Output

2

Demo

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.