简体   繁体   中英

How do I format JSON date array object using PHP

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

Initial date is in Ymd and I need to format them to dmY 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"

}

]

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"
  }
]

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']);

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 dmy

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM