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

spkevin's avatar

Date Validation issues

Hey Team.

I'm trying to get user input as a date. DD/MM/YYYY style. I have an BookingRequest class which contains rules

return [
            'room_number' => 'required|numeric',
            'email' => 'required|email',
            'check_in' => 'required|date_format:dd/mm/yyyy|
                           after:today',
            'check_out' => 'required|date_format:dd/mm/yyyy|
                            after:check_in'
        ];

I keep on getting: The check in does not match the format dd/mm/yyyy. when inputting something like 05/09/2017.

I'm referring to this page for date formats http://php.net/manual/en/datetime.formats.date.php

Where am I going wrong?

0 likes
6 replies
ejdelmonico's avatar

Standard input for a date is yyyy-mm-dd so I would do the initial validation in the input field using the pattern and value attributes.

For example:

<input type="text" pattern="(?:19|20)[0-9]{2}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-9])|(?:(?!02)(?:0[1-9]|1[0-2])-(?:30))|(?:(?:0[13578]|1[02])-31))" required />

Have a look here: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/date

1 like
JackJones's avatar
use Carbon\Carbon;

Carbon::createFromFormat('d/m/Y|', $request->input('check_in'))->toDateTimeString();
1 like
Snapey's avatar

use date format in the validator, and then in the model, create a mutator to handle the date format

this will allow you to just save date to the model

    public function setDateAttribute($value)
    {
        $this->attributes['date'] = $this->parseDate($value);
    } 

    public function parseDate($date=null)
    {
        if(isset($date))
        {
            return Carbon::createFromFormat('d M Y',$date);
                                   //or whatever format you are using
        }
        return null;
    }
JackJones's avatar

One thing I noticed, and I don't know if it would ever be an issue (probably not), but if you use createFromFormat(), it automatically fills in the time based on the current time, unless you use a pipe after the format, e.g. d m Y|, then it sets everything after the pipe to 0

Please or to participate in this conversation.