How to Provide Validation in date?


#1

I want to put validation in date, how do I do that? I have a form where I enter my travel date and return date, but the travel date should not be changed 24hrs before the travel date that is booked.
Please suggest me how to do in php and js.


#2

So, you need to take the “todays date” and subtract 1 day from it. Then do a comparison.


#3

In my case for now, I am not able to book return ticket once the trip has started. I need an idea where a person can change his/her return date once his departure date has started.


#4

Then you have something limiting that. What are the business rules? Where do you enforce them?


#5
function dateDifference($day_1, $day_2) {
$diff = strtotime($day_1) - strtotime($day_2);
$sec = $diff % 60;
$diff = intval($diff / 60);
$min = $diff % 60;
$diff = intval($diff / 60);
$hours = $diff % 24;
$days = intval($diff / 24);   
//if travel hours is there increase the days
if($days > 0 && $hours > 0){
    $days += 1;
} else {
    //if days are zero change it as 1        
    $days = 1;
}
return array('sec' => $sec, 'min' => $min, 'hrs' => $hours, 'days' => $days);

}


#6

Why are you rolling your own dataDifference? That functionality already exists.

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');

#7

This was already written by someone else that is why I am unable to make changes. I will try and see with the already existing function. @astonecipher


#8

So somewhere around where that function is currently called, it does a check to see if the value can be altered. That is where you need to focus.