Jump to content

[SOLVED] Get the smallest date out of 4 dates


ireneyee

Recommended Posts

$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.

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>";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.