Jump to content

Archived

This topic is now archived and is closed to further replies.

phpeveryday

Calculating the subset of time.

Recommended Posts

Hi All,

 

I'm trying to find a way to calculate the total time in seconds of when 3 different times intersect each other.

 

e.g.

 

Start Time End Time Duration 2013-10-08 08:29:52 2013-10-08 08:32:46 00:02:54 2013-10-08 08:31:57 2013-10-08 08:32:46 00:00:49 2013-10-08 08:32:06 2013-10-08 08:36:28 00:04:22

 

From my calculation it will 85 seconds of overlapping time.

 

Can anyone assist?

Share this post


Link to post
Share on other sites
Start Time                      End Time                       Duration

2013-10-08 08:29:52      2013-10-08 08:32:46     00:02:54

2013-10-08 08:31:57      2013-10-08 08:32:46     00:00:49

2013-10-08 08:32:06      2013-10-08 08:36:28     00:04:22

 

 

Hopefully the above displays better.

Share this post


Link to post
Share on other sites

Example 1:

<?php
$start_TimeStr="2013-10-08 08:29:52";
$end_TimeStr = "2013-10-08 08:32:46";
$Duration = strtotime($end_TimeStr) - strtotime($start_TimeStr);
echo $Duration; // 174 sec

Share this post


Link to post
Share on other sites

Hi jazzman1,

 

Thanks for your response.

 

I was more looking at the subset or intersection of the the three rows where the End Time of all the rows do not exceed the End Time of the first row.

 

Actually my initial calculation of 85 seconds is incorrect. I believe it should be 40 seconds.

 

Here is my logic:-

 

From the first row the duration is 00:02:54 or 174 seconds.

The second row the Start and End time falls within the first Start and End and the duration is 49 seconds and is the total subset time used at this time.

The third row starts within the second row but ends after the second row so the duration will be a difference of 2013-10-08 08:32:06  and  2013-10-08 08:32:46 which is 40 seconds.

 

I hope I am a bit clearer.

Share this post


Link to post
Share on other sites

Perhaps...

 

new_start_time would be LATEST start time of the three

new_end_time would be EARlIEST end time of the three

those two would be the common intersection/duration of the three

(would apply to more or less time sequences)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.