search

Home  >  Q&A  >  body text

Parse carbon element in date format

I'm trying to parse a date in the following format:

2017-09-20T10:59:10.0000000 01:00

I'm using Carbon, so I tried:

Carbon::createFromFormat('Y-m-dTH:i:s.u vP', $date)

Which output:

The timezone could not be found in the databasen
Unexpected data found.n
Data missing

I guess the last timezone parameter may be wrong, but I can't find how to parse that date format: /

thanks for your help!

P粉337385922P粉337385922522 days ago780

reply all(1)I'll reply

  • P粉236743689

    P粉2367436892023-10-22 10:44:17

    You need to add a symbol to the time zone, for example:

    +01:00

    Then this will work for you:

    Carbon::createFromFormat('Y-m-d\TH:i:s.0000000 P', $date)

    If your string can contain -01:00 but instead of 01:00 you get 01:00, please execute Do the following first:

    $timezone = str_after($date, ' ');
    if ($timezone[0] !== '-') {
        $date = str_before($date, ' ') . ' +' . $timezone;
    }

    reply
    0
  • Cancelreply