skip to Main Content

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-dTH:i:s.uP', '2022-06-21T05:52:46.648533678Z');
Carbon::createFromFormat('Y-m-dTH:i:s.vP', '2022-06-21T05:52:46.648533678Z');

And always get an error:

The timezone could not be found in the database

2

Answers


  1. 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-dTH:i:s.u???P', '2022-06-21T05:52:46.648533678Z');
    
    Login or Signup to reply.
  2. 'Y-m-dTH:i:s.u???P' format as per RiggsFolly’s solution is the way if you expect only this format.

    If you want to allow more or less digits, note that parse can also do it without having to specify any format:

    echo Carbon::parse('2022-06-21T05:52:46.648533678Z');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search