Reputation: 35
My issue is that I have array how contain value with dateTime format but I would like modify this value with a format Y-m-d. I tried a lot of thing of issue here but nothing work when I encode my json.
Now I have that :
[contacts] => Array (
[0] => Array (
[createdAt] =>2020-01-29T17:00:04.159+01:00
)
)
And I would result like
[contacts] => Array (
[0] => Array (
[createdAt] =>2020-01-29
)
)
My php code is :
$contacts=$result[contacts];
foreach ($contacts as $contact) {
$time = new DateTime($contact['createdAt']);
$date = $time->format('Y-m-d');
echo '<br>' .$date; //that show the date format I want
}
//that don't show correct format
$json=json_encode($contacts);
print_r($json);
Someone can help me ?
Upvotes: 3
Views: 170
Reputation: 105
You can reassign value in your contact array by just doing below changes in your code
foreach ($contacts as $key => $contact) {
$time = new DateTime($contact['createdAt']);
$date = $time->format('Y-m-d');
echo '<br>' .$date; //that show the date format I want
$contacts[$key]['createdAt'] = $date;
}
it will reassign your new date value in your existing element
Upvotes: 0