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 Quote Link to comment 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. Quote Link to comment 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>"; } ?> Quote Link to comment 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; ?> Quote Link to comment Share on other sites More sharing options...
ireneyee Posted July 21, 2008 Author Share Posted July 21, 2008 Hey thanks guys!! I got it! Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.