note 98605 deleted from datetime.diff by danbrown
| From: | danbrown@php.net | Date: | Fri, 25 Jun 2010 20:07:23 +0000 |
| Subject: | note 98605 deleted from datetime.diff by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-170473@lists.php.net to get a copy of this message | ||
Note Submitter: Sergio Abreu
----
/* Sergio Abreu's datediff
* Contact: http://sites.sitesbr.net
* Function s_datediff means (Special or Sergio Abreu's) datediff
* Returns decimal portions of two different datetimes.
* Converts string representations of date automatically.
* First param examples:
"s" for seconds
"i" for minutes
"h" for hours
"d" for days
"m" for months
"y" for years
Shownegatives is optional, and default is false, to use absolute function on the result (never
negative).
*/
function s_datediff( $str_interval, $dt_menor, $dt_maior, $shownegative=false){
if( is_string( $dt_menor)) $dt_menor = date_create( $dt_menor);
if( is_string( $dt_maior)) $dt_maior = date_create( $dt_maior);
$diff = date_diff( $dt_menor, $dt_maior, ! $shownegative);
switch( $str_interval){
case "y":
$total = $diff->y + $diff->m / 12; break;
case "m":
$total= $diff->y * 12 + $diff->m + $diff->d/30;
break;
case "d":
$total = $diff->y * 365.25 + $diff->m * 30 + $diff->d + $diff->h/24;
break;
case "h":
$total = ($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h +
$diff->i/60;
break;
case "i":
$total = (($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 + $diff->h)
* 60 + $diff->i + $diff->s/60;
break;
case "s":
$total = ((($diff->y * 365.25 + $diff->m * 30 + $diff->d) * 24 +
$diff->h) * 60 + $diff->i)*60 + $diff->s;
break;
}
if( $diff->invert) return -1 * $total;
else return $total;
}
Enjoy, and comment if you liked.