Is somehow possible to change output of json_encode(['date' => $dateTimeObj])?
Now it prints
{
"date": {
"date": "2016-10-27 11:23:52.000000",
"timezone_type": 3,
"timezone": "Europe/Paris"
}
}
I would like to have output like this
{
"date": "2016-10-27T11:23:52+00:00"
}
My first idea was to create my own DateTime class which will extends DateTime and override jsonSerialize, but DateTime does not implement JsonSerializable interface and __toString did not help too.
I'm using PHP 7.0.8.
I meant something like this
<?php
MyDateTime extends \DateTime implements jsonSerialize
{
public function jsonSerialize() // this is never called
{
return $this->format("c");
}
}
$datetime = new MyDatetime();
$output = [
'date' => $datetime; // want to avoid $datetime->format("c") or something like this everywhere
];
json_encode($output);
This code now output
{
"date": {
"date": "2016-10-27 11:23:52.000000",
"timezone_type": 3,
"timezone": "Europe/Paris"
}
}
I would like to have
{
"date": "2016-10-27T11:23:52+00:00"
}
json_encode()does not manipulate any data you give it. If you put the date into the object/array you are encoding the way you want to see it it will stay that way. So fix the code that adds the datejson_encodeit.2016-10-27 11:23:52.000000, I'm guessingyyyy-MM-dd HH:mm:ss ???what's the last.000000?Y-m-d H:i:s.uformat according to us1.php.net/manual/en/function.date.php