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

adamjhn's avatar

The date does not match the format d F Y - H:i.

Im getting this errror when updating a post:

The date does not match the format d F Y - H:i.

Do you know where is the issue?

input html:

<div class="form-group col-md-6">
    <label for="date">Date</label>
    <div class="input-group date" data-provide="datepicker">
        <input type='text' onkeydown="event.preventDefaultdate
                       name="date" value="{{old('registration_type_start_date')}}"
               class="form-control" placeholder="DD/MM/YYY" />
        <span class="input-group-addon"><i class="fa fa-calendar text-primary" aria-hidden="true"></i></span>
    </div>
</div>

jQuery:

  $( ".input-group.date" ).datetimepicker({
    format: "dd MM yyyy - hh:ii",
    autoclose: true,
    todayBtn: true,
    minuteStep: 5
});

update method:

 public function update(Request $request, $id){
        $this->validate($request, [
            ....
            'date' => 'required|date_format:d F Y - H:i',
        ]);
        $post->date = Carbon::createFromFormat('d F Y - H:i', $request->date);
        $post->save();
        return redirect()->back();
    }
0 likes
28 replies
Cronix's avatar

Your code isn't showing, but I'm suspecting that it has something to do with your validation rules for the date. It's expecting it in a format that it isn't being sent from the form.

1 like
Cronix's avatar

Try putting quotes around the date_format rule.

date_format:"d F Y - H:i"
1 like
Cronix's avatar

Personally I'd just be sending it in the format that you are saving it as, instead of sending it in a specific format, verifying it, converting it to how you want to save it and then saving it. If you send it in the standard "YYYY-MM-DD HH:mm:ss" format, then all you'd need to do is

$this->validate($request, [
    ....
    'date' => 'required|date',
]);

//$post->date = Carbon::createFromFormat('d F Y - H:i', $request->date);
$post->save();
return redirect()->back();
2 likes
Hadayat's avatar

@Cronix worked like a charm. it's very simpler than format the front and back end format

adamjhn's avatar

Thanks, but also dont works with "date_format:"d F Y - H:i". Using only date then I cannot use the format 'd F Y - H:i'. Or using date like "'required|date'," then is possible to show in other format?

rin4ik's avatar

try this

'date' => 'required|date_format:"d-m-Y H:i"'
1 like
Cronix's avatar

What is the field type that you are storing this date in the database? I don't understand why you are sending it to your controller in that weird format, converting it and then storing it. I use that plugin and it works fine when doing what I said in my last post.

1 like
rin4ik's avatar

I don't understand what F means?

adamjhn's avatar

The format in the database is "Datetime". But so it is stored like " 'date' => 'required|date'," and then to show in another format its just to use carbon in the frontend?

Cronix's avatar
Cronix
Best Answer
Level 67

Yes, convert it to how you want to show it on the front end.

{{ $something->date->format('d F Y - H:i') }}

So try changing this:

$( ".input-group.date" ).datetimepicker({
    format: "YYYY-MM-DD HH:mm:ss",
    autoclose: true,
    todayBtn: true,
    minuteStep: 5
});

And then use

$this->validate($request, [
    ....
    'date' => 'required|date',
]);

$post->save();
return redirect()->back();

Note: in order to change the format when displaying it, it needs to be a carbon instance. So you should include date as a $date mutator in your model.

https://laravel.com/docs/5.6/eloquent-mutators#date-mutators

1 like
adamjhn's avatar

Thanks I change the jQuery and also in the controller but appears "The ticket type date is not a valid date."

I have this jquery to load some values in form fields:

                    $("input[name='date']").val( moment(data.date).format('DD MMMM YYYY - HH:mm'));
               

Do you know if can be because of that?

adamjhn's avatar

It seems that is because now if I select a date in the date picker it appears in the input this "YYYY-April-Sunday 12:12:00". Maybe is because of this format:

  $( ".input-group.date" ).datetimepicker({
                format: "YYYY-MM-DD HH:mm:ss",
                autoclose: true,
                todayBtn: true,
                minuteStep: 5
            });

Cronix's avatar

what does the date look like when it gets to the controller?

put this as the first line of the controller dd($request->date);

rin4ik's avatar

I think you have to leave format empty

 $("input[name='date']").val( moment(data.date).format());
adamjhn's avatar

It appears like" 02 Abril 2018 - 23:11"".

Cronix's avatar

Somethings wrong here.

If this is set as the date format in jquery, YYYY-MM-DD HH:mm:ss, then it should't be displaying YYYY-April-Sunday 12:12:00 in the controller.

YYYY-MM-DD HH:mm:ss would be something like 2018-04-10 14:25:01

1 like
adamjhn's avatar

Thanks, but If I have this format "YYYY-MM-DD HH:mm:ss" in the jquery. When I click in the datepicker input it appears "YYYY-April-Monday 01:04:13".

adamjhn's avatar

Resume of the code:

Html Input:

<div class="form-group col-md-6">
    <label for="ticket_type_start_date">Ticket Type Start Date</label>
    <div class="input-group date" data-provide="datepicker">
        <input type='text' onkeydown="event.preventDefault()"
               name="ticket_type_start_date" value="{{old('ticket_type_start_date')}}"
               class="form-control" placeholder="DD/MM/YYY" />
        <span class="input-group-addon"><i class="fa fa-calendar text-primary" aria-hidden="true"></i></span>
    </div>
</div>

jQuery:

  $( ".input-group.date" ).datetimepicker({
    format: "dd MM yyyy - hh:ii",
    autoclose: true,
    todayBtn: true,
    minuteStep: 5
});

...            
// some jquery to load the form fields with some values when the correspondend radio button is selected    
 $("input[name='ticket_type_start_date']").val( moment(data.ticket_type_start_date).format('DD MMMM YYYY - HH:mm'));

Controller:

public function update(Request $request, $id){
        //dd($request->ticket_type_start_date);
        //dd($request->all());

        $this->validate($request, [
            'ticket_type_start_date' => 'required|date',
            'ticket_type_end_date' => 'required|date|after_or_equal:ticket_type_start_date',
        ]);

        $ticketTypeToUpdate = TicketType::find($request->radiobutton);
        ....
        $ticketTypeToUpdate->start_date = $request->ticket_type_start_date;
        $ticketTypeToUpdate->end_date = $request->ticket_type_end_date;


        $ticketTypeToUpdate->save();


        return redirect()->back();
    }
rin4ik's avatar
 $("input[name='date']").val( moment(data.date).format(''MM-DD-YYYY HH:mm:ss'));

you will get this 02-03-2018 23:11:01

1 like
Cronix's avatar

You've got 3 different date formats you're working with, and they are not all the same.

moment.js dates (formatting is different)

format: "dd MM yyyy - hh:ii",
$("input[name='date']").val( moment(data.date).format('DD MMMM YYYY - HH:mm'));

and in the validation, you are checking for php's date, which will only be valid if it can run it through strtotime successfully.

It would have been helpful if you included everything you were doing, such as where you are setting the date in the timepicker, from the getgo.

1 like
adamjhn's avatar

Where to check where setting the date? I didnt understnad the date is set using the date input using the datetimepicker plugin.

Cronix's avatar

Here, you are setting the date in the timepicker from data (I assume) you passed to the view.

$("input[name='date']").val( moment(data.date).format('DD MMMM YYYY - HH:mm'));

It does not match the format here:

$( ".input-group.date" ).datetimepicker({
    format: "dd MM yyyy - hh:ii",
    autoclose: true,
    todayBtn: true,
    minuteStep: 5
});
1 like
jlrdw's avatar

This has become another goodie, slipping of shoes, and I am popping me some popcorn. OP pick a js technology.

1 like

Please or to participate in this conversation.