kevinfwb Posted March 15, 2007 Share Posted March 15, 2007 I have 5 variables $date1, $date2, $date3, $date4 and $date5. I need a way to analyze the 5 dates and find the one that is next from the today's date. I'm sure it's fairly basic, I just don't have the slighest idea of where to begin. Thanks, -Kevin Link to comment https://forums.phpfreaks.com/topic/42785-date-comparison/ Share on other sites More sharing options...
kenrbnsn Posted March 15, 2007 Share Posted March 15, 2007 Convert the dates to UNIX timestamps, put them into an array, sort them, compare each one in ascending order with today's UNIX timestamp. The first one that is greater than or equal to today's timestamp is the one you want: <?php $dates = array(strtotime($date1),strtotime($date2),strtotime($date3),strtotime($date4),strtotime($date5)); sort($dates); $now = time(); // Today's timestamp $foundit = false; for($i=0;$i<count($dates);$i++) { if ($dates[$i] >= $now && !$foundit) { $target = $dates[$i]; $foundit = true; } } echo 'The next date is ' . date('l , F jS, Y',$target); ?> Note: untested. Ken Link to comment https://forums.phpfreaks.com/topic/42785-date-comparison/#findComment-207688 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.