Intersection of 2 times in php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tomas

    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

  • NC

    #2
    Re: Intersection of 2 times in php

    Tomas wrote:[color=blue]
    >
    > 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.[/color]

    $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($time 1_from);
    $ts2 = strtotime($time 1_to);
    $ts3 = strtotime($time 2_from);
    $ts4 = strtotime($time 2_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

    Comment

    • Tomas

      #3
      Re: Intersection of 2 times in php

      THANKS!!!

      Comment

      Working...