DevMan DevMan - 1 year ago 73
PHP Question

Comparing start time and end time

I know there are many questions asked about this topic here but no one is about the problem I have.

This script is for reservations where the user selects the date and the start and end time and makes a reservation.
I have a form with a date selector input field and two time selector input fields, one for the start time and one for the end time.

The problem is that the store which I'm writing the reservation script for is opened from 17:00 evening to 01:00 morning. So if someone is reserving from 23:00 to 01:00 the start time is always shown as bigger, which results in that the form is not validated.

Does anyone know if there is a solution to this or if there is a validator out there which can do this.

NOTE: I only want to compare the times and I don't want to add another date field.

enter image description here

var timeto=$('#timeto').val();
var timefrom=$('#timefrom').val();
alert('start time should be smaller')

So if
time from
is 23:00 and
time to
is 00:00 than the alert is shown,but in reality 00:00 is a greater time than 23:00

Answer Source

Just subtract one hour while creating object.

var timefrom = new Date();
temp = $('#timefrom').val();.split(":");
timefrom.setHours((parseInt(temp[0]) - 1 + 24) % 24);

var timeto = new Date();
temp = $('#timeto').val().split(":");
timeto.setHours((parseInt(temp[0]) - 1 + 24) % 24);

if (timeto < timefrom) 
    alert('start time should be smaller');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download