AdRock Posted April 26, 2008 Share Posted April 26, 2008 I have a form where i have 4 fields 1 for the start month and 1 for the start year 1 for the end month and 1 for the end year I am trying to validate it so the the end month/year can't be before the start month/year I have all the other form validation working but need some advice how i can do this last bit Do I have to convert these form values into a date format for comparison and if so, how? Link to comment https://forums.phpfreaks.com/topic/103075-comparing-dates/ Share on other sites More sharing options...
BlueSkyIS Posted April 26, 2008 Share Posted April 26, 2008 one way: $start_time = mktime(0,0,0,$start_month, 1, $start_year); $end_time = mktime(0,0,0,$end_month,1, $end_year); if ($end_time < $start_time) { die("end time is before start time"); } Link to comment https://forums.phpfreaks.com/topic/103075-comparing-dates/#findComment-527959 Share on other sites More sharing options...
Fadion Posted April 27, 2008 Share Posted April 27, 2008 An identical approach, but using strtotime(): <?php $startTime = strtotime($_POST['startyear'] . - . $_POST['startmonth']); $endTime = strtotime($_POST['endyear'] . - . $_POST['endmonth']); if($endTime < $startTime){ echo 'End time should be less then start time.'; } else{ //other code } ?> Link to comment https://forums.phpfreaks.com/topic/103075-comparing-dates/#findComment-528060 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.