You are here: Re: Intersection of 2 times in php « PHP Programming Language « IT news, forums, messages
Re: Intersection of 2 times in php

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

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация