I do not know of any way to create custom% custom, but I can suggest an alternative using timestamps.
Convert your date to a timestamp ( timezones
), via int
( if your date is in the format strtotime
) or string
(if it is in this format) :
$timestamp1 = strtotime($hora1);
$timestamp2 = $hora2->getTimestamp();
Adjust the difference in seconds for one of them (or both):
function ajustar($timestamp, $horas = 0, $minutos = 0, $segundos = 0)
{
return $timestamp + ($segundos + 60 * ($minutos + 60 * $horas);
}
$timestamp1_ajustado = ajustar($timestamp1);
$timestamp2_ajustado = ajustar($timestamp1, 0, 4);
Convert them back to DateTime::getTimestamp
, using DateTime
:
$hora1_ajustada = new DateTime();
$hora1_ajustada->setTimestamp($timestamp1_ajustado);
$hora2_ajustada = new DateTime();
$hora2_ajustada->setTimestamp($timestamp2_ajustado);
Now you can calculate the difference between them normally. Complete Code:
function diferenca_ajustada($hora1, $hora2, $horas = 0, $minutos = 0, $segundos = 0)
{
$timestamp1 = $hora1->getTimestamp();
$timestamp2 = $hora2->getTimestamp() + $segundos + 60*($minutos + 60*$horas);
$ajustada1 = new DateTime();
$ajustada1->setTimestamp($timestamp1);
$ajustada2 = new DateTime();
$ajustada2->setTimestamp($timestamp2);
return $ajustada1->diff($ajustada2);
}
Example in PHPFiddle . Note: If the dates are in different timezones, you may need to first convert them to UTC before comparing them:
function para_utc($timestamp)
{
return $timestamp - date("Z", $timestamp);
}