Created
July 18, 2013 19:32
-
-
Save lelotnk/6032287 to your computer and use it in GitHub Desktop.
Calcular Diferença de Horas entre Datas
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static function getIntervalUnits($interval, $unit) | |
{ | |
// Day | |
$total = $interval->format('%a'); | |
if ($unit == 'days') | |
return $total; | |
//hour | |
$total = ($total * 24) + ($interval->h ); | |
if ($unit == 'hours') | |
return $total; | |
//min | |
$total = ($total * 60) + ($interval->i ); | |
if ($unit == 'minutes') | |
return $total; | |
//sec | |
$total = ($total * 60) + ($interval->s ); | |
if ($unit == 'seconds') | |
return $total; | |
return false; | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$dataEntrada = '18/07/2013 15:00'; | |
$dataSaida = '23/07/2013 23:00'; | |
$format = 'd/m/Y H:i'; | |
$timeZone = new DateTimeZone('America/Sao_Paulo'); | |
$entrada = DateTime::createFromFormat($format, $dataEntrada, $timeZone); | |
// echo $entrada->format('Y-m-d'); | |
$saida = DateTime::createFromFormat($format, $dataSaida, $timeZone); | |
// echo $saida->format('Y-m-d'); | |
$interval = $entrada->diff($saida); | |
echo $interval->format('%a dias and %h horas') . '<br />'; | |
$diasTotal = $interval->days; | |
$horas = $interval->h; | |
$horasTotal = ($diasTotal * 24) + $horas; | |
echo 'Total de ' . $horasTotal . ' horas.'; | |
echo '<br />'; | |
// var_dump($interval); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment