I looked a couple of questions with similar title but my case is quiet different. I am trying to move data from one table to another using laravel task. The table from which I want to copy the data from is using varchar for date while the table I am copying to has a date column type. So I must convert the string to valid carbon date before inserting into the new table. Unfortunately the old table has the dates mixed in different format so I have to check and convert before insert but I keep getting this error when dealing with a particular string format
Exception : DateTime::__construct(): Failed to parse time string (26/02/1991) at position 0 (2): Unexpected character
Here's what my code for converting the strings to date format looks like
if ($user->profile->date_of_birth === null) {
$dob = null;
Log::info([$user->profile->date_of_birth, $dob]);
} else if (Carbon::parse($user->profile->date_of_birth)->toDateString() == true) {
$dob = Carbon::parse($user->profile->date_of_birth)->toDateString();
Log::info([$user->profile->date_of_birth, $dob]);
} else if (Carbon::createFromFormat('d/m/Y', $user->profile->date_of_birth)->format('Y-m-d') == true) {
$formattedDate = Carbon::createFromFormat('d/m/Y', $user->profile->date_of_birth)->format('Y-m-d');
$dob = Carbon::parse($formattedDate)->toDateString();
Log::info([$user->profile->date_of_birth, $dob]);
}
Using laravel's artisan tinker, I am actually able to convert the string to date format as can be seen here

Why do I keep getting an error when I run my task and it encounters that particular string?