I'll-Be-Back
I'll-Be-Back

Reputation: 10828

Carbon - today or future date only

I am having problem with validating the $pickUpDate date. When I select today date and submit - I get an error: Pick-up: today or future date only

It should allow me to use today or future date only. It should NOT allow less than today date.

public function withValidator($validator)
{
    $validator->after(function ($validator) {
        $pickUpDate = Carbon::createFromFormat('D j M Y', $this->pickup_date);
        $dropOffDate = Carbon::createFromFormat('D j M Y', $this->dropoff_date);

        $todayDate = Carbon::now();

        if (!$pickUpDate->gte($todayDate)) {
            $validator->errors()->add('pickup_date', 'Pick-up: today or future date only');
        }
    });
}

Upvotes: 1

Views: 6397

Answers (1)

Buglinjo
Buglinjo

Reputation: 2076

As today() will return start time of today you will need to add ->endOfDay() in the end.

Carbon::now()->endOfDay();

or add minutes:

Carbon::now()->addMinutes(5);

In your case, you are adding now() in the function wich checks if this time is past. As this function takes time to check (maybe milliseconds) it will return false. add 1 minute to your now carbon object and it will be fine.

Upvotes: 2

Related Questions