php - DateTime Diff having inconsistencies in Months -


having problem difference in months, function below returns age month

function agemonths($dob) {     //new date time $dob must y-m-d format     $dobobject = datetime::createfromformat('y-m-d',$dob);      //current date     $nowobject = new datetime();      //difference     $diff = $nowobject->diff($dobobject);     return $diff->y.'.'.$diff->m; } 

problem having inconsistencies in months exact not, please @ sample below

5.5 - 2008-10-03 correct 8.2 - 2005-12-27 must 8.3 4.5 - 2009-09-24 must 4.6 6.7 - 2007-07-14 must 6.8 6.7 - 2007-07-17 must 6.8 6.5 - 2007-09-28 must 6.6 7.1 - 2007-01-17 must 7.2 7.1 - 2007-02-10 correct 6.3 - 2007-11-16 must 6.4 7.1 - 2007-02-10 correct 6.3 - 2007-11-16 must 6.4 6.10 - 2007-05-08 correct 

i cannot find wrong, ideas?

there no rounding going on, it's notion of "months difference" not same 1 used dateinterval.

subtracting 2 datetime values imbues resulting dateinterval "months" value equal number of whole (in calendar sense) months of difference between dates. example, in calendar terms 2013-03-11 0 months away 2013-04-01 (2013-04-11 1 month away).

what want not months of difference calendar-wise, arithmetic difference of month indexes. example, 2013-03-11 has month difference of 1 2013-04-01 (because 4 - 3 = 1).

so, desired result way:

$dobobject = datetime::createfromformat('y-m-d',$dob); $nowobject = new datetime();  return $dobobject->format('n') - $nowobject->format('n'); // might negative 

see difference live.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -