1

I am trying to get date values from a JSON in PHP:

{
  "data": [
    {
      "start": {
        "date": "2019-12-27 21:05:28.073000",
        "timezone_type": 3,
        "timezone": "UTC"
      },
      "final": {
        "date": "2019-12-28 20:59:43.153000",
        "timezone_type": 3,
        "timezone": "UTC"
      }
    }
  ]
}

My PHP code is trying to read the JSON but I guess that I do not reach the start and final sublevels.

I get an error and nothing is returned.

I am trying to get in one variable "2019-12-27 21:05:28 UTC+3" (start) and in another one "2019-12-28 20:59:43 UTC+3" (final)

<?php 
$json=file_get_contents("https://XXX/myjson.php");
$data =  json_decode($json);
if (count($data->data)) {
    // Open the table
    // Cycle through the array
    foreach ($data->data as $idx => $stand) {
        echo "first operation ".$stand->start."<br>";   
        echo "final operation ".$stand->final;   
    }}
?>

1 Answer 1

1

Problem is in your mistake of object element giving. see my code:

$json='{
  "data": [
    {
      "start": {
        "date": "2019-12-27 21:05:28.073000",
        "timezone_type": 3,
        "timezone": "UTC"
      },
      "final": {
        "date": "2019-12-28 20:59:43.153000",
        "timezone_type": 3,
        "timezone": "UTC"
      }
    }
  ]
}';
$data =  json_decode($json);
if (count($data->data)) {
// Open the table
// Cycle through the array
foreach ($data->data as $idx => $stand) {
    if(isset($stand->start)){
        echo "first operation ".$stand->start->date."<br>";  
    }else{
        echo "final operation ".$stand->final->date;   
    }

}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.