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

lionslair's avatar

Validating Date and Time fields

I am trying to find a way to validate a two date and time fields

The fields stored in the datebase are

  • state_datetime
  • end_datetime

However in the form submission the input is split into

start_datetime d F Y start_time H:i A

end_datetime d F Y end_time H:i A

What I am trying to achieve in the Request validation is that the end time is not before the start time and vise versa.

However I have tried every permutation I can think of

'on_sale_datetime' => 'required|date_format:"d F Y h:i A"|before:' . str_replace(',', '', $this->off_sale_datetime) . ' '. $this->off_sale_time, 'off_sale_datetime' => 'required|date_format:"d F Y h:i A"|after:' . str_replace(',', '', $this->on_sale_datetime) . ' '. $this->on_sale_time,

This fails because the submitted fields start_datetime is the format of d F Y and not d F Y h:i A

I have tried

$this->merge([
        'combined_start_datetime' => str_replace(',', '', $this->start_datetime) . ' '. $this->start_time,
        'combined_end_datetime' => str_replace(',', '', $this->end_datetime) . ' '. $this->end_time,
    ]);

And added the fields combined_start_datetime and combined_end_datetime to guarded in the model. However then the validation is skipped.

If I try

$this->merge([
        'start_datetime' => str_replace(',', '', $this->start_datetime) . ' '. $this->start_time,
        'end_datetime' => str_replace(',', '', $this->end_datetime) . ' '. $this->end_time,
    ]);
    

Then on failure the value of start_datetime back in the form is d F Y h:i A not d F Y

Is there any solution to validating these input for what I am trying to achieve?

0 likes
1 reply

Please or to participate in this conversation.