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

ptgokulrajan's avatar

Date in Form Requests

Hi,

Is there any way in which we could validate the date is between two particular dates. For Example; Given an input date I want it to be between 1st of July to 31st of July. The start and end dates are generated based on the current date.

0 likes
4 replies
handy_man's avatar

Using a carbon date you would want to compare to see something like the following

$today = Carbon::now()->format('Y-m-d');
$july_start = '2015-07-01';
$july_end = '2015-07-31';

if ($july_start <= $today && $july_end >= $today )
{
true
}
else
{
false
}

ptgokulrajan's avatar

Thanks for your replies.

What i want to accomplish is something like this:

I have a form request in which i am providing the validation rules. Now for validating date, I am using before and after validators. My problem is, the date I want to pass to the before and after validators are calculated from today either by subtracting or adding days to the current date. After that,I am passing the modified date to the validator.

public function rules() { $date_i_want = Carbon::now()->subDays(15); return [ 'date'=>'required|before:${date_i_want}' ]; }

But this doesn't seem to be working. Is there any way i could pass the variable to the before validator.

Thanks

billmurrin's avatar

@ptgokulrajan. Perhaps this will work, worked when I tested it out in a test environment.

    public function rules()
    {
        $before_date = Carbon::now()->subDays(15)->toDateString();
        $after_date = Carbon::now()->addDays(15)->toDateString();

        return [
            'date' => 'required|date|before:' . $after_date . '|after:' . $before_date,
        ];
    }
1 like

Please or to participate in this conversation.