Home > Date Time, PHP > PHP Years Months Days Difference Between Dates

PHP Years Months Days Difference Between Dates

$date1 = “2011-03-26”;
$date2 = “2011-04-25”;
$yMdDiffArray = yearMonthDaysDifference($date1,$date2);
printf(“%d years, %d months, %d days\n”, $yMdDiffArray[‘years’], $yMdDiffArray[‘months’], $yMdDiffArray[‘days’]);

function yearMonthDaysDifference($fromDate,$todate)
{
$dateArray = explode(‘-‘,$todate);
$plusOne = 1;
$to_date = Date(‘Y-m-d’,mktime(0,0,0,$dateArray[1],$dateArray[2]+$plusOne,$dateArray[0]));
$diff = abs(strtotime($to_date) – strtotime($fromDate));
$years = floor($diff / (365*60*60*24));
$months = floor(($diff – $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($diff – $years * 365*60*60*24 – $months*30*60*60*24)/ (60*60*24));
$differenceArray = array(‘years’=>$years,’months’=>$months,’days’=>$days);
return $differenceArray;
}

Advertisements
Categories: Date Time, PHP Tags:
  1. No comments yet.
  1. No trackbacks yet.

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: