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

ethar's avatar
Level 5

Could not parse '29/10/1948': Failed to parse time string (29/10/1948)

i try to insert '29/10/1948' in date field, but i got this error Could not parse '29/10/1948': Failed to parse time string (29/10/1948) at position 0 (2): Unexpected character

in model i use casts

protected $casts = [ 'in_date'=>'date'];

and in insert i try

$model->in_date = \Carbon\Carbon::parse($request->in_date)->format('d-m-Y');

but not working

0 likes
6 replies
LaryAI's avatar
Level 58

The error message suggests that there is an issue with parsing the date string '29/10/1948'. The format 'd-m-Y' is being used to parse the date, but the actual format of the date string is 'd/m/Y'. To fix this, you need to update the format used in the parse method.

Here's the updated code:

$model->in_date = \Carbon\Carbon::createFromFormat('d/m/Y', $request->in_date)->format('Y-m-d');

This code uses the createFromFormat method of Carbon to parse the date string with the correct format 'd/m/Y'. Then, it formats the date as 'Y-m-d' before assigning it to the in_date attribute of the model.

Make sure to replace $request->in_date with the actual variable that holds the date string in your code.

click's avatar

29/10/1948 is not a normal (valid) date format.

There are 3 options:

  1. You should enter your date as mm/dd/yyyy so 10/29/1948
  2. Use a more standard format, something like: 'dd-mm-yyyy' so 29-10-1989 or any other standard supported format.
  3. You specify the date format yourself Carbon::createFromFormat('d/m/Y', '29/10/1948');
Snapey's avatar

Always try and use createFromFormat.

Carbon can't know how to parse 03/04/2023 for example (3rd April or 4th March?)

Please or to participate in this conversation.