note 97326 deleted from function.date-diff by danbrown

From: Date: Wed, 14 Apr 2010 18:04:33 +0000
Subject: note 97326 deleted from function.date-diff by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-168241@lists.php.net to get a copy of this message
Note Submitter: gm at dynamit dot us ---- <? echo "<br/>Test1: "; echo date_diff64("2099-12-31 00:00:00.000"); echo "<br/>Test2: "; echo date_diff64("2099-12-31 00:00:00.000","2099-12-30 23:00:00"); echo "<br/>Test3: "; echo date_diff64("2999-12-31 00:00:00.000","1901-01-01"); echo "<br/>Test4: "; echo date_diff64("1901-01-01 00:00:00.000","2999-12-31"); /********************************************** * Compare two dates and return the difference * in seconds. For PHP 5.3.0 and beyond. */ function date_diff64($a, $b="NOW") { $ret = 0; $a = new DateTime($a); $b = ($b=="NOW")?new DateTime():new DateTime($b); $delta = $a->diff($b); $days = $delta->format("%a"); $hours = $delta->format("%h"); $mins = $delta->format("%m"); $secs = $delta->format("%s"); $ret = ($days * 24 * 3600) + ($hours * 3600) + ($mins * 60) + $secs; if ( $a < $b ) { $ret *= -1; } return $ret; } ?> Example output: * Test1: 2831105332 * Test2: 3600 * Test3: 34681046400 * Test4: -34681046400

« previous php.notes (#168241) next »