Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

dr24's avatar
Level 2

Problem with updating date from form to database

I am trying to update date_of birth column in database and when I submit my form I get this error

DateTime::__construct(): Failed to parse time string (25/03/1995) at position 0 (2): Unexpected character

Now in my blade I formated date of birth to show d/m/Y and when updating I think it updates Y/m/d, because when I remove format function from my blade it works fine. So I need help on how to update with format('d/m/Y') in my database and how to validate it properly in my form request validation. Any help is appreciated. Here is my code.

index.blade.php

<input type="text" placeholder="dd/mm/yyyy" name="date_of_birth" value="{{ $userForShowProfile->date_of_birth ? $userForShowProfile->date_of_birth->format('d/m/Y') : "" }}">

UserController.php

public function updateProfileCharacteristics(UpdateProfileCharacteristicsRequest $request)
{
     $user = Auth::user();

     $user->update(
     [
        'date_of_birth' => $request->date_of_birth,
        'age' => Carbon::now()->diffInYears($request->date_of_birth),
        'updated_at' =>  Carbon::now()
     ]
     );

     return redirect()->route('profile.show', [$user->username]);
}

UpdateProfileCharacteristicsRequest.php

public function rules()
{
    return [
        'date_of_birth' => ['date'],
    ];
}
0 likes
7 replies
MichalOravec's avatar

@dr24 change your UserController.php like this

public function updateProfileCharacteristics(UpdateProfileCharacteristicsRequest $request)
{
    $user = Auth::user();

    $user->update([
        'date_of_birth' => Carbon::parse($request->date_of_birth)->toDateString(),
        'age' => Carbon::now()->diffInYears($request->date_of_birth),
        'updated_at' =>  Carbon::now()
    ]);

    return redirect()->route('profile.show', [$user->username]);
}
dr24's avatar
Level 2

@michaloravec

I get the same error

DateTime::__construct(): Failed to parse time string (25/03/1980) at position 0 (2): Unexpected character
MichalOravec's avatar

@dr24 OK so

public function updateProfileCharacteristics(UpdateProfileCharacteristicsRequest $request)
{
    $user = Auth::user();

    $dateOfBirth = Carbon::parse($request->date_of_birth);

    $user->update([
        'date_of_birth' => $dateOfBirth->toDateString(),
        'age' => Carbon::now()->diffInYears($dateOfBirth),
        'updated_at' => Carbon::now()
    ]);

    return redirect()->route('profile.show', [$user->username]);
}
dr24's avatar
Level 2

@michaloravec

Also if it is any help my date_of_birth column is of type date and in my User model I put date_of_birth in protected $fillable variable and protected $dates variable

MichalOravec's avatar
Level 75

@dr24 This could work

public function updateProfileCharacteristics(UpdateProfileCharacteristicsRequest $request)
{
    $user = Auth::user();

    $dateOfBirth = Carbon::createFromFormat('d/m/Y', $request->date_of_birth);

    $user->update([
        'date_of_birth' => $dateOfBirth->toDateString(),
        'age' => Carbon::now()->diffInYears($dateOfBirth),
        'updated_at' => Carbon::now()
    ]);

    return redirect()->route('profile.show', [$user->username]);
}
1 like

Please or to participate in this conversation.