php-carbon

I got Not enough data available error with Carbon createFromFormat method


I try to parse with carbon datetime value in sstring as "2020-09-17T14:08:59Z", but with format :

Carbon::createFromFormat($stringData,'Y-m-d H:i:s' )

I got error :

Not enough data available to satisfy format {"exception":"[object] (Carbon\\Exceptions\\InvalidFormatException(code: 0): The format separator does not match

Which format have I to use ?

Thanks in advance!


Solution

  • Wrong parameters order, and wrong format (\T instead of space to match your separator, and e to get the timezone)

    Carbon::createFromFormat('Y-m-d\TH:i:se', $stringData)
    

    Or you can simply let Carbon find out the format:

    Carbon::parse($stringData)