1

I need to format all the date_of_birth attributes of the JSON array using a PHP foreach loop.

Initial date is in Y-m-d and I need to format them to d-m-Y using PHP only.

I have tried it but maybe I am not doing something right.

<?php

// Convert json from objects to array
$characters = json_decode(file_get_contents('data.json'), true);

//Loop through array
foreach ($characters as $key => $value) {
if (in_array($key, ['date_of_birth'])) {
$oDate = DateTime::createFromFormat('Y-m-d', '1988-08-21');
    $characters[$key]['date_of_birth'] = $oDate->format('d-m-Y');
    }
}

file_put_contents('results_new.json', json_encode($characters));


print_r($characters);



?>

 //JSON data
[
{
    "first_name"    : "Andy",
    "last_name"     : "James",
    "date_of_birth" : "1988-08-21",
    "date_of_move"  : "2000-09-11"
},

{
    "first_name"    : "Laura",
    "last_name"     : "Simmons",
    "date_of_birth" : "1968-04-09",
    "date_of_move"  : "2010-09-05"
},

{
   "first_name"    : "Jeff",
    "last_name"     : "Bridge",
    "date_of_birth" : "1980-02-15",
    "date_of_move"  : "1990-08-08"

}

]

4 Answers 4

2

For some reason, the way you check for the 'date_of_birth' key causes problems. I've managed to get everything working in a foreach() loop using $value with a reference. Please try the below:

<?php

// Convert json from objects to array
$characters = json_decode(file_get_contents('data.json'), true);

//Loop through array
foreach ($characters as $key => &$value) {
    if (array_key_exists('date_of_birth', $value)) {
        $oDate = DateTime::createFromFormat('Y-m-d', $value['date_of_birth']);
        $value['date_of_birth'] = $oDate->format('d-m-Y');
    }
}

file_put_contents('results_new.json', json_encode($characters));

print_r($characters);

Output:

[
  {
    "first_name": "Andy",
    "last_name": "James",
    "date_of_birth": "21-08-1988",
    "date_of_move": "2000-09-11"
  },
  {
    "first_name": "Laura",
    "last_name": "Simmons",
    "date_of_birth": "09-04-1968",
    "date_of_move": "2010-09-05"
  },
  {
    "first_name": "Jeff",
    "last_name": "Bridge",
    "date_of_birth": "15-02-1980",
    "date_of_move": "1990-08-08"
  }
]
Sign up to request clarification or add additional context in comments.

4 Comments

Legend mate. Cheers!!
@LeoG my pleasure! I've updated the answer with cleaned up code and removed a duplicate line.
@mkveksas my edit is wrong, I didn't notice the true arguments for json_decode. I rollback.
@Cid I tried to amend the previous edit but didn't notice you submitted a new one! Apologies.
0

Try changing this line:

$oDate = DateTime::createFromFormat('Y-m-d', '1988-08-21');

With this one:

$oDate = DateTime::createFromFormat('Y-m-d', $value['date_of_birth']);

2 Comments

Thanks for the answer. It does work but only partially. It formats the first date_of_birth, but does not format the remaining two date_of_births.
Are you sure? Try deleting results_new.json file and try again.
0

You have to use strtotime() :

for($i=0;$i<count($characters);$i++) {
    $characters[$i]['date_of_birth'] = date("d-m-Y", strtotime($characters[$i]['date_of_birth']));
}

Here date format update to d-m-y

Comments

0

in_array($key, ['date_of_birth']) isn't working as you expect. Try this way :

$json = '[
{
    "first_name"    : "Andy",
    "last_name"     : "James",
    "date_of_birth" : "1988-08-21",
    "date_of_move"  : "2000-09-11"
},
{
    "first_name"    : "NO DATE",
    "last_name"     : "NO DATE"
},
{
    "first_name"    : "Laura",
    "last_name"     : "Simmons",
    "date_of_birth" : "1968-04-09",
    "date_of_move"  : "2010-09-05"
},

{
   "first_name"    : "Jeff",
    "last_name"     : "Bridge",
    "date_of_birth" : "1980-02-15",
    "date_of_move"  : "1990-08-08"

}]';

$decoded = json_decode($json);

foreach ($decoded as $key => $value)
{
    if (isset($value->date_of_birth))
    {
        $value->date_of_birth = DateTime::createFromFormat('Y-m-d', $value->date_of_birth)->format('d-m-Y');
    }
}

var_dump($decoded);

Outputs :

array (size=4)
  0 => 
    object(stdClass)[1]
      public 'first_name' => string 'Andy' (length=4)
      public 'last_name' => string 'James' (length=5)
      public 'date_of_birth' => string '21-08-1988' (length=10)
      public 'date_of_move' => string '2000-09-11' (length=10)
  1 => 
    object(stdClass)[2]
      public 'first_name' => string 'NO DATE' (length=7)
      public 'last_name' => string 'NO DATE' (length=7)
  2 => 
    object(stdClass)[3]
      public 'first_name' => string 'Laura' (length=5)
      public 'last_name' => string 'Simmons' (length=7)
      public 'date_of_birth' => string '09-04-1968' (length=10)
      public 'date_of_move' => string '2010-09-05' (length=10)
  3 => 
    object(stdClass)[4]
      public 'first_name' => string 'Jeff' (length=4)
      public 'last_name' => string 'Bridge' (length=6)
      public 'date_of_birth' => string '15-02-1980' (length=10)
      public 'date_of_move' => string '1990-08-08' (length=10)

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.