php - How to express the difference between two dates in a human-readable format -


if have 2 dates - $end_date , $start_date, how can express difference between 2 in format such "2 years : 4 months : 2 days"?

i know can difference between 2 dates so:

$dif=strtotime($end_date)-strtotime($today); 

but how can convert result human-readable format, shown above?

this how can format timestamp:

echo date('d-m-y', strtotime($end_date)) // dd-mm-yyyy format 

are looking calculate difference between 2 dates, in number days?

edit: code find difference between dates in "xxyear yymonth zzday". code assumes start , end dates in yyyy-mm-dd format. if that's not case you, please either change them yyyy-mm-dd format, or change arguments mktime() accordingly.

$enddate = '2011-03-01'; $startdate = '2011-02-02'; $daysperyear = 365; $dayspermonth = 30.4; $diffday = $diffmonth = $diffyear = 0;  $enddatets = mktime(0, 0, 0, substr($enddate, 5, 2), substr($enddate, 8, 2), substr($enddate, 0, 4)); $startdatets = mktime(0, 0, 0, substr($startdate, 5, 2), substr($startdate, 8, 2), substr($startdate, 0, 4)); $diffday = ($enddatets - $startdatets) / 60 / 60/ 24;   // difference between 2 dates in number of days $diffyear = floor($diffday / $daysperyear); // difference in years $diffday = $diffday % $daysperyear; // balance days $diffmonth = floor($diffday / $dayspermonth);   // difference in months $diffday = ceil($diffday % $dayspermonth); // balance days echo ($diffyear ? $diffyear . 'year ' : '') . ($diffmonth ? $diffmonth . 'month ' : '') . ($diffday ? $diffday . 'day' : ''); 

note: haven't tested code possible date combinations, including leap year etc. please feel free tweak needed.

hope helps.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -