Days/Months/Years Between in two Date

Below the code will illustrate, how to find the days,months and years between two selected dates.

The below code has  the example of two dates to show the Days/Month/Years Between on those,

$date = “06-29-1980”;
$currentDate = mktime();    
$tdate=’2009-04-16′;       

$dateSplit = explode(“-“, $date);

//$dateSplit[0] = Month
//$dateSplit[1] = Day
//$dateSplit[2] = Year

$previousDate = mktime(0, 0, 0, $dateSplit[0], $dateSplit[1], $dateSplit[2]);

$nrSeconds = $currentDate – $previousDate;

$nrSeconds = abs($nrSeconds);
$nrDaysPassed = floor($nrSeconds / 86400);
$nrWeeksPassed = floor($nrSeconds / 604800);
$nrYearsPassed = floor($nrSeconds / 31536000);

echo $nrDaysPassed . ” days have passed between ” . date(“F j, Y”, $previousDate) . ” and ” . date(“F j, Y”, $currentDate) . “<br />”;
echo $nrWeeksPassed . ” weeks have passed between ” . date(“F j, Y”, $previousDate) . ” and ” . date(“F j, Y”, $currentDate) . “<br />”;
echo $nrYearsPassed . ” years have passed between ” . date(“F j, Y”, $previousDate) . ” and ” . date(“F j, Y”, $currentDate) . “<br />”;

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: