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.

Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.