note 53670 added to ref.datetime

From: Date: Wed, 08 Jun 2005 18:49:50 +0000
Subject: note 53670 added to ref.datetime
Groups: php.notes 
Request: Send a blank email to php-notes+get-90136@lists.php.net to get a copy of this message
This dateDiff() function can take in just about any timestamp, including UNIX timestamps and anything that is accepted by strtotime(). It returns an array with the ability to split the result a couple different ways. I built this function to suffice any datediff needs I had. Hope it helps others too. <?php /********* dateDiff() function ********** * returns Array of Int values for difference between two dates * $date1 > $date2 --> positive integers are returned * $date1 < $date2 --> negative integers are returned * * $split recognizes the following: * 'yw' = splits up years, weeks and days (default) * 'y' = splits up years and days * 'w' = splits up weeks and days * 'd' = total days * * examples: * $dif1 = dateDiff() or dateDiff('yw') * $dif2 = dateDiff('y') * $dif3 = dateDiff('w') * $dif4 = dateDiff('d') * * assuming dateDiff returned 853 days, the above * examples would have a print_r output of: * $dif1 == Array( [y] => 2 [w] => 17 [d] => 4 ) * $dif2 == Array( [y] => 2 [d] => 123 ) * $dif3 == Array( [w] => 121 [d] => 6 ) * $dif4 == Array( [d] => 847 ) * * note: [h] (hours), [m] (minutes), [s] (seconds) are always returned as elements of the Array */ function dateDiff($dt1, $dt2, $split='yw') { $date1 = (strtotime($dt1) != -1) ? strtotime($dt1) : $dt1; $date2 = (strtotime($dt2) != -1) ? strtotime($dt2) : $dt2; $dtDiff = $date1 - $date2; $totalDays = intval($dtDiff/(24*60*60)); $totalSecs = $dtDiff-($totalDays*24*60*60); $dif['h'] = $h = intval($totalSecs/(60*60)); $dif['m'] = $m = intval(($totalSecs-($h*60*60))/60); $dif['s'] = $totalSecs-($h*60*60)-($m*60); // set up array as necessary switch($split) { case 'yw': # split years-weeks-days $dif['y'] = $y = intval($totalDays/365); $dif['w'] = $w = intval(($totalDays-($y*365))/7); $dif['d'] = $totalDays-($y*365)-($w*7); break; case 'y': # split years-days $dif['y'] = $y = intval($totalDays/365); $dif['d'] = $totalDays-($y*365); break; case 'w': # split weeks-days $dif['w'] = $w = intval($totalDays/7); $dif['d'] = $totalDays-($w*7); break; case 'd': # don't split -- total days $dif['d'] = $totalDays; break; default: die("Error in dateDiff(). Unrecognized \$split parameter. Valid values are 'yw', 'y', 'w', 'd'. Default is 'yw'."); } return $dif; } ?> ---- Manual Page -- http://www.php.net/manual/en/ref.datetime.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+53670 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53670&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53670&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53670&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53670&report=yes&reason=useless Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53670&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+53670&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#90136) next »