juniordev1
juniordev1

Reputation: 21

Validate form so End Date can not be before Start Date. - PHP

I have this part of a form where it shows end start and end date. I have validated it so that if no date is entered then an error message will show asking the use to enter a date, however, I need it to also check that the end date is not before the start date and echo an error asking the user to make sure the end date is after the start date if this happens. It's probably something very simple but I just can't figure out the correct code. Thanks.

// event start date
echo "<li class='cf'>";
echo "<label for='startdate'>Start date</label>";
echo "<div class='input'>";
echo "<input name='startdate' class='eventdate' placeholder='DD/MM/YYYY' type='date' value='";
if (!is_null($postedData['startdate'])) { echo $postedData['startdate']; };
echo "' />";
if($failedData['startdate']) { echo "<p class='error'>Please enter the date of your event.</p>";    }   
echo "</div>";
echo "</li>";

echo $postedData['startdate'];

// event end date
echo "<li class='cf'>";
echo "<label for='enddate'>End date</label>";
echo "<div class='input'>";
echo "<input name='enddate' class='eventdate' placeholder='DD/MM/YYYY' type='date' value='";
if (!is_null($postedData['date'])) { echo $postedData['date']; };
echo "' />";
if($failedData['enddate']) { echo "<p class='error'>Please enter the date of your event.</p>"; }    

echo "</div>";
echo "</li>";

echo $postedData['enddate'];

echo "<li class='cf'>";
echo "<label for='timestart'>Event time</label>";
echo "<div class='input inputs'>";

Upvotes: 1

Views: 1553

Answers (1)

IUW
IUW

Reputation: 441

You can get a simple idea

$a = "2014-01-01";
$b = "2014-02-01";
$date1 = strtotime($a);
$date2 = strtotime($b);

if($date1 > $date2){
  // Date one is >
}
else{
  // Date 2 is >
}

Date can be formatted in php

Upvotes: 2

Related Questions