# Json:serialize should get DateTime object as string

**URL:** <https://discourse.laminas.dev/t/json-serialize-should-get-datetime-object-as-string/3660>\
**Category:** Components & MVC\
**Tags:** laminas-hydrator, laminas-json\
**Created:** [April 4, 2024, 11:37am UTC](https://discourse.laminas.dev/t/json-serialize-should-get-datetime-object-as-string/3660 "2024-04-04T11:37:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![modir](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/modir/32/930_2.png) [@modir](https://discourse.laminas.dev/u/modir)\
**Post date:** [April 4, 2024, 11:37am UTC](https://discourse.laminas.dev/t/json-serialize-should-get-datetime-object-as-string/3660/1 "2024-04-04T11:37:28Z")

</div>

I have here an object and one of its properties is a `DateTime` object. Now I want to use `Json::serialize()` to get the JSON representation of it. Is there a way to instruct `Json::serialize()` to get the `DateTime` object as a `string`? The idea is that automatically `->format('c')` would be called on the `DateTime` object.

Is this possible?

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [April 4, 2024, 12:04pm UTC](https://discourse.laminas.dev/t/json-serialize-should-get-datetime-object-as-string/3660/2 "2024-04-04T12:04:25Z")

</div>

You can use a hydrator with a strategy for dates before converting the data to JSON.

Otherwise the PHP function `json_encode` already converts the `DateTime` object:

```php
$object = new stdClass();
$object->artist = 'David Bowie';
$object->title = 'Modern Love';
$object->duration = '4:46';
$object->album = 'Let\'s Dance';
$object->release_date = new DateTime('1983-04-14');

$json = json_encode($object);
var_dump($json);

```

```json
{
    "artist": "David Bowie",
    "title": "Modern Love",
    "duration": "4:46",
    "album": "Let\u0027s Dance",
    "release_date": {
        "date": "1983-04-14 00:00:00.000000",
        "timezone_type": 3,
        "timezone": "UTC"
    }
}

```

---

<div class="post-metadata">

**Author:** ![modir](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/modir/32/930_2.png) [@modir](https://discourse.laminas.dev/u/modir)\
**Post date:** [April 4, 2024, 12:10pm UTC](https://discourse.laminas.dev/t/json-serialize-should-get-datetime-object-as-string/3660/3 "2024-04-04T12:10:18Z")

</div>

In this case the hydrator because I need it like this:

`"release_date": "1983-04-14 00:00:00.000000",`

and not with a sub-object.

Thank you for both ideas. I didn’t know that json\_encode() does it automatically.

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [April 4, 2024, 12:21pm UTC](https://discourse.laminas.dev/t/json-serialize-should-get-datetime-object-as-string/3660/4 "2024-04-04T12:21:52Z")

</div>

> [@modir](#):
>
> I didn’t know that json\_encode() does it automatically.

You can ignore laminas-json, it was a good solution in the past because it provided a fallback:

> **ext/json**
> 
> By default, the above two calls will proxy to the `json_decode()` and `json_encode()` functions of `ext/json`, which is bundled in default installations of PHP. Using laminas-json, however, ensures that the functionality works regardless of whether or not the extension is available. Additionally, the component provides some features not found in `ext/json`, such as encoding native JSON expressions, communicating class inheritance, and customizations around pretty printing.

> **[Basic Usage - laminas-json - Laminas Docs](https://docs.laminas.dev/laminas-json/basics/#basic-usage)**
>
> De/Serialize JSON in PHP, including JavaScript expressions

> <https://github.com/laminas/laminas-json/blob/53ff787b20b77197f38680c737e8dfffa846b85b/src/Json.php#L335-L351>

---

<div class="post-metadata">

**Author:** ![froschdesign](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/froschdesign/32/38_2.png) [@froschdesign](https://discourse.laminas.dev/u/froschdesign)\
**Post date:** [April 4, 2024, 12:28pm UTC](https://discourse.laminas.dev/t/json-serialize-should-get-datetime-object-as-string/3660/5 "2024-04-04T12:28:06Z")

</div>

Example:

```php
$object = new stdClass();
$object->title = 'Modern Love';
$object->duration = '4:46';
$object->artist = 'David Bowie';
$object->album = 'Let\'s Dance';
$object->release_date = new DateTime('1983-04-14');

$hydrator = new Laminas\Hydrator\ObjectPropertyHydrator();
$hydrator->addStrategy(
    'release_date',
    new Laminas\Hydrator\Strategy\DateTimeFormatterStrategy(
        'Y-m-d'
    )
);

$values = json_encode($hydrator->extract($object), JSON_PRETTY_PRINT);
var_dump($values);

```

```json
{
    "title": "Modern Love",
    "duration": "4:46",
    "artist": "David Bowie",
    "album": "Let's Dance",
    "release_date": "1983-04-14"
}

```

---

<div class="post-metadata">

**Author:** ![modir](https://yyz2.discourse-cdn.com/flex032/user_avatar/discourse.laminas.dev/modir/32/930_2.png) [@modir](https://discourse.laminas.dev/u/modir)\
**Post date:** [April 4, 2024, 2:27pm UTC](https://discourse.laminas.dev/t/json-serialize-should-get-datetime-object-as-string/3660/6 "2024-04-04T14:27:57Z")

</div>

Thank you very much. That was indeed helpful.
