Search code examples
phpdatelaravellaravel-5php-carbon

Format date string in carbon in laravel5?


I want to convert $date='01-07-2010' in to 2010-07-01

My following code is not wokring,

Carbon::createFromFormat('Y-m-d', $date);

It throws error,

InvalidArgumentException in Carbon.php line 414: Trailing data

How can I solve this?


Solution

  • If your date format is d-m-Y then

    Carbon::createFromFormat('Y-m-d', $date);
    

    won't work, because you're not specifying the correct format; you need

    Carbon::createFromFormat('d-m-Y', $date);
    

    The format that you specify as the date-format for your value must match the actual format of the date string that you want to convert to a Carbon object