ireneyee Posted July 18, 2008 Share Posted July 18, 2008 Hi all, I'm currently facing this problem here where I got 4 dates and out of these 4 dates I need to get the latest date. Can anyone help?? Thanks Link to comment https://forums.phpfreaks.com/topic/115371-solved-get-the-smallest-date-out-of-4-dates/ Share on other sites More sharing options...
samshel Posted July 18, 2008 Share Posted July 18, 2008 $dtDate1 = "2005-09-13"; $dtDate2 = "2006-09-13"; $dtDate3 = "2007-09-13"; $dtDate4 = "2008-09-13"; $arrDates = array(strtotime($dtDate1), strtotime($dtDate2), strtotime($dtDate3),strtotime($dtDate4)); sort($arrDates); echo $arrDates[3]; // will display latest date "2008-09-13" echo $arrDates[0]; // will display oldest date "2005-09-13" PS: code not tested. Link to comment https://forums.phpfreaks.com/topic/115371-solved-get-the-smallest-date-out-of-4-dates/#findComment-593136 Share on other sites More sharing options...
marcus Posted July 18, 2008 Share Posted July 18, 2008 Yep, just convert the dates to timestamps and sort them. <?php $date = strtotime(date("M d, y")); $dates[] = strtotime("+3 days", $date); $dates[] = strtotime("+1 week", $date); $dates[] = strtotime("+1 year", $date); $dates[] = strtotime("+1 month", $date); sort($dates); $closest = $dates[0]; $farthest = $dates[3]; echo date("M d, y", $closest); // plus 3 days echo "<br>"; echo date("M d, y", $farthest); // plus 1 year echo "<br><br>"; // now all the dates foreach($dates AS $dated){ echo date("M d, y", $dated) . "<br>"; } ?> Link to comment https://forums.phpfreaks.com/topic/115371-solved-get-the-smallest-date-out-of-4-dates/#findComment-593140 Share on other sites More sharing options...
Barand Posted July 18, 2008 Share Posted July 18, 2008 <?php $dtDate[] = "2005-09-13"; $dtDate[] = "2008-09-14"; $dtDate[] = "2007-09-13"; $dtDate[] = "2008-09-13"; $latest = max ($dtDate); echo $latest; ?> Link to comment https://forums.phpfreaks.com/topic/115371-solved-get-the-smallest-date-out-of-4-dates/#findComment-593156 Share on other sites More sharing options...
ireneyee Posted July 21, 2008 Author Share Posted July 21, 2008 Hey thanks guys!! I got it! Link to comment https://forums.phpfreaks.com/topic/115371-solved-get-the-smallest-date-out-of-4-dates/#findComment-595249 Share on other sites More sharing options...
Third_Degree Posted July 21, 2008 Share Posted July 21, 2008 Thank you Barand for having some sense and showing the most efficient way of doing this. Link to comment https://forums.phpfreaks.com/topic/115371-solved-get-the-smallest-date-out-of-4-dates/#findComment-595256 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.