How can it be done in PHP to get the number of hours from a range of dates that meet a time interval? For example, having the range of dates:
SUBIDA = 2017-01-02 01:00 - BAJADA = 2017-01-02 22:00
And knowing that the intervals are:
DE 00:00 a 06:00 y DE 22:00 a 23:59
The total hours of the range that apply between these intervals must be found. For the case of the example, with the provided data should be obtained:
DE 00:00 a 06:00 -> 5 horas
DE 22:00 a 23:59 -> 2 horas
I'm using this code for the moment:
if ($h1<$h2)
{
$res2=MIN($h2,'23:59:59')-MAX($h1,'22:00:00');
$res1=MIN($h2,'06:00:00')-MAX($h1,'00:00:00');
}
else
{
$res2=MAX(0,('23:59:59')-($h1))+MAX(0,$h2-('22:00:00'));
$res1=MAX(0,('06:00:00')-($h1))+MAX(0,$h2-'00:000:00');
}
Where, $ h1 = upload in 'H: i: s' format and $ h2 = download in 'H: i: s' format.
The code works for me but not very precise, since it sometimes returns me 1 or 2 hours less in some interval.
Does anyone have a better way to do it? Thanks in advance.
Reginaldo Bray