473,327 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Intersection of 2 times in php

Hello

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.

Im unable to solve this problem, so I'm pleasing you for help.

A big thank for the solver in advance!!!

Tomas Srna

Feb 6 '06 #1
2 2123
NC
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

Feb 6 '06 #2
THANKS!!!

Feb 6 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Antoine Logean | last post by:
Hi, What is the easiest way to get the intersection of two strings in python (a kind a "and" operator) ? ex: string_1 =...
17
by: Gordon Williams | last post by:
Hi, I have to lists that I need to find the common numbers (2nd rounded to nearest integral) and I am wondering if there is a more efficient way of doing it. >>> a= >>> b= >>> ...
3
by: Alain Frisch | last post by:
Hello, The following rule in the XML Schema spec, section "Schema Component Constraint: Attribute Wildcard Intersection" seems strange to me: ======================================= 3 If...
4
by: Sebastian Cohen S | last post by:
Hello, I am new to SQL and currently using Access 2003 and need a little help on the following. I have two queries each contains one column formed with a string that is very similar (only...
6
by: kimos | last post by:
hi all, how to calculate the intersection of 2 rectangle a rectangle is the following: Rectangle makeRectangle (Point lowerLeft, Point upperRight) { Rectangle r;
3
by: cai_rongxi | last post by:
Hi, Can some body share the code to find the intersection of two rectangles? Thanks in advance
2
by: sicapitan | last post by:
Hi There :) Is it possible to get the 4 corners where 2 dives intersect? I'm making a table-ish system and som drag and drop elements need to snap to the area intersecting between 2 divs. I...
2
by: mkppk | last post by:
I have kind of strange change I'd like to make to the sets.Set() intersection() method.. Normally, intersection would return items in both s1 and s2 like with something like this: ...
11
by: Prateek | last post by:
I have 3 variable length lists of sets. I need to find the common elements in each list (across sets) really really quickly. Here is some sample code: # Doesn't make sense to union the sets -...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.