note 97050 deleted from datetime.diff by danielc
| From: | danielc@php.net | Date: | Mon, 19 Apr 2010 16:12:41 +0000 |
| Subject: | note 97050 deleted from datetime.diff by danielc | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-168421@lists.php.net to get a copy of this message | ||
Note Submitter: yhbeh888 at yahoo dot com
----
The following function is developed to address the issue rased by andarin2@yahoo.com on 18-Nov-2009
10:32 regarding the inconsitencies of the PHP function date_diff() or DateTime::diff(). Both $dt1
and $dt2 are input DateTime objects.
function my_date_diff( $dt1, $dt2, $flag='second')
{
$d = $dt2->getTimestamp() - $dt1->getTimestamp();
switch ( $flag ) {
case 'DAY':
case 'Day':
case 'day':
return $d / 86400;
case 'HOUR':
case 'Hour':
case 'hour':
return $d / 3600;
case 'MINUTE':
case 'Minute':
case 'minute':
return $d / 60;
case 'SECOND':
case 'Second':
case 'second':
return $d;
default:
print_message( "No unit is specified ... Return time difference in seconds.",
'warning');
return $d;
}
}