|
Posted by NC on 02/06/06 18:42
Tomas wrote:
>
> I have to do a script that calculates the intersection of 2 times. I
> write it in php instead of explaining:
>
> $time1_from = "2006-02-01 08:00";
> $time1_to = "2006-02-01 20:00";
>
> $time2_from = "2006-02-01 06:00";
> $time2_to = "2006-02-01 18:00";
>
> I want the script to calculate that there are 8 hours common in those
> two times. And the script should work for any 2 times. And of course,
> where there is no intersection, the return value should be 0.
$time1_from = "2006-02-01 08:00";
$time1_to = "2006-02-01 20:00";
$time2_from = "2006-02-01 06:00";
$time2_to = "2006-02-01 18:00";
$ts1 = strtotime($time1_from);
$ts2 = strtotime($time1_to);
$ts3 = strtotime($time2_from);
$ts4 = strtotime($time2_to);
$overlap = max($ts1, $ts3) - min($ts2, $ts4);
if ($overlap < 0) {
$overlap = 0;
}
echo 'The overlap between the two time periods is ',
$overlap / 3600,
' hours.';
Cheers,
NC
[Back to original message]
|