Search code examples
phpdatetimephp-carbon

How to create Carbon object from this string: 2022-06-21T05:52:46.648533678Z?


How to create DateTime object with PHP from this string: 2022-06-21T05:52:46.648533678Z? There is nine numbers after 'dot', after seconds.

I tried to use next variants:

<?php
Carbon::createFromFormat('Y-m-d\TH:i:s.uP', '2022-06-21T05:52:46.648533678Z');
Carbon::createFromFormat('Y-m-d\TH:i:s.vP', '2022-06-21T05:52:46.648533678Z');

And always get an error:

The timezone could not be found in the database

Solution

  • The format should be as follows to cope with the 9 numerics after the . PHP only can cope with 6 numerics so the ? are just place holders for anything

    <?php
    Carbon::createFromFormat('Y-m-d\TH:i:s.u???P', '2022-06-21T05:52:46.648533678Z');