note 97511 deleted from function.date by danbrown

From: Date: Fri, 23 Apr 2010 13:42:09 +0000
Subject: note 97511 deleted from function.date by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168560@lists.php.net to get a copy of this message
Note Submitter: ray(at)uktaxcalculators(dot)co(dot)uk ---- I made a function to return the difference between two dates in the following string format: X Years, X Months, X Days. It seems to work well, but there is a problem around leap years which results in an extra day being added. Can anybody clean it up for me! Thanks, Ray Arman <?php //function created to give a string of the difference between two dates function stringDateDiff($beginDate, $endDate) //dates in Y-m-d format { $datestart=explode("-", $beginDate); $dateend=explode("-", $endDate); $startm=$datestart[1]; $startd=$datestart[2]; $starty=$datestart[0]; $endm=$dateend[1]; $endd=$dateend[2]; $endy=$dateend[0]; $diffyears = date('Y',strtotime(($endy)."-".$endm."-".$endd) -strtotime($starty."-".$startm."-".$startd)) -1970; if($diffyears<0){$diffyears=0;} if($diffyears>0) { $diffmonths=date('n',strtotime(($endy+1)."-".$endm."-".$endd) - strtotime($endy."-".$startm."-".$startd))-1; $diffdays=date('j',strtotime(($endy+1)."-".$endm."-".$endd)- strtotime($endy."-".$startm."-".$startd))-1; } else { $diffmonths=date('n',strtotime(($endy)."-".$endm."-".$endd)- strtotime($endy."-".$startm."-".$startd))-1; $diffdays=date('j',strtotime(($endy)."-".$endm."-".$endd) -strtotime($endy."-".$startm."-".$startd))-1; } $difference="$diffyears ".($diffyears==1?'Year':'Years').", $diffmonths ".($diffmonths==1?'Month':'Months').", $diffdays ".($diffdays==1?'Day':'Days'); return $difference; } ?>

« previous php.notes (#168560) next »