php - Laravel 4.2 Date Validation

760

How do I validate a date in such a way that it only accepts today and any date older than today? I knowbefore:today accepts older dates whileafter: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.

578

Answer

Solution:

From my understanding, you want theend_at to on the same date or afterstart_at but not beforestart_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, Theend_at need to be relevant with thestart_at (same or after thestart_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 thestrtotime PHP function.

People are also looking for solutions to the problem: php - Sending Long Description Along with Attachment using SMTP in Codeigniter

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.