Search code examples
phplaraveldatephp-carbon

Change Date format using Carbon


I am using Laravel framework and for date conversion using Carbon package

I am unable convert date format to mysql format.I have following code

$request->event_start_date will have 25/08/2017

print_r(carbon::parse($request->event_start_date));

when $request->event_start_date is 03/08/2017 then it will print as

Carbon\Carbon Object( [date] => 2017-03-08 00:00:00.000000 [timezone_type] => 3 [timezone] => UTC)

But if date is 25/08/2017 then it will throw erorr as

"G:\XAMPP\htdocs\myproject\vendor\nesbot\carbon\src\Carbon\Carbon.php" line : 291 message : "DateTime::__construct(): Failed to parse time string (25/08/2017) at position 0 (2): Unexpected character"

need to convert 25/08/2017 to Mysql date format.I have tried a lot to fix this .finaly posted here so that i get some help from you

Thanks


Solution

  • Carbon extends PHP's native DateTime class, so you can use the same createFromFormat method:

    $dateString = '25/08/2017';
    $dateObject = \Carbon::createFromFormat('d/m/Y', $dateString);