note 114355 deleted from datetime.diff by googleguy
| From: | googleguy@php.net | Date: | Wed, 12 Feb 2014 19:10:54 +0000 |
| Subject: | note 114355 deleted from datetime.diff by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-198481@lists.php.net to get a copy of this message | ||
Note Submitter: taroksing at gmail dot com
----
As I needed not only to get the hours difference but also the arithmetic sign I just made this
simple function:
<?php
function calcDiffHoras($fecha1, $fecha2 = null)
{
$fecha1 = new DateTime($fecha1);
if(!$fecha2)
{
$fecha2 = new Datetime();
}
else
{
$fecha2 = new Datetime($fecha2);
}
$diff = date_diff($fecha1,$fecha2,true);
$horas = $diff->h + ($diff->days*24);
if($fecha1 < $fecha2)
{
//negative value
$horas = -1 * abs($horas);
}
else
{
$horas = $horas;
}
return $horas;
}
?>