Change output of DateTime in json_encode
<p>Is somehow possible to change output of <code>json_encode(['date' =&gt; $dateTimeObj])</code>? </p>

<p>Now it prints </p>

<pre><code>{
    "date": {
        "date": "2016-10-27 11:23:52.000000",
        "timezone_type": 3,
        "timezone": "Europe/Paris"    
    }
}
</code></pre>

<p>I would like to have output like this</p>

<pre><code>{
    "date": "2016-10-27T11:23:52+00:00"
}
</code></pre>

<p>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.</p>

<p>I'm using PHP 7.0.8.</p>

<p>I meant something like this</p>

<pre><code>&lt;?php    
MyDateTime extends \DateTime implements jsonSerialize 
{
    public function jsonSerialize() // this is never called
    {
       return $this-&gt;format("c");
    }
}

$datetime = new MyDatetime();

$output = [
    'date' =&gt; $datetime;  // want to avoid $datetime-&gt;format("c") or something like this everywhere
];

json_encode($output);
</code></pre>

<p>This code now output</p>

<pre><code>{
    "date": {
        "date": "2016-10-27 11:23:52.000000",
        "timezone_type": 3,
        "timezone": "Europe/Paris"    
    }
}
</code></pre>

<p>I would like to have</p>

<pre><code>{
    "date": "2016-10-27T11:23:52+00:00"
}
</code></pre>