Richie
Richie

Reputation: 1439

Laravel 4.2 Date Validation

How do I validate a date in such a way that it only accepts today and any date older than today? I know before:today accepts older dates while after:today accepts dates starting tomorrow on-wards.

 $rules = [
    'start_at'      => 'required|date|date_format:Y-m-d|after:yesterday',
    'end_at'        => 'required|date|date_format:Y-m-d|after:start_at',
];

I want to be able to start a task today and end today.

Upvotes: 2

Views: 1637

Answers (1)

mininoz
mininoz

Reputation: 5958

From my understanding, you want the end_at to on the same date or after start_at but not before start_at.

public function rules()
    {
        return [
            'start_at'  => 'required|date|date_format:Y-m-d|after:yesterday',
            'end_at'    => 'required|date|date_format:Y-m-d|after:' . \Carbon\Carbon::parse($this->start_date)->subDay()->toDateString(),
        ];
    }

What I have done so far, The end_at need to be relevant with the start_at (same or after the start_at).

  1. I'm using Carbon to convert start_at into date.
  2. Subtract date by 1 day.
  3. Convert it back to string according to the strtotime PHP function.

Upvotes: 6

Related Questions