Jump to content

find how many hours in a given timespan


Zaynt

Recommended Posts

Hi,

 

I was wondering how I can find out how many hours there is in the timespan eg. 12pm - 4pm when the only information I have is a starting time and a stop time.

 

example:

 

start: 8am

stop: 1pm

 

there is 1 hour in the timspan 12pm - 4pm.

 

Anyone know how I make a script that can calculate this in php?

Link to comment
https://forums.phpfreaks.com/topic/89840-find-how-many-hours-in-a-given-timespan/
Share on other sites

Well converting to 24 hour military time is where you want to be. Just do 24 our time and it's just simple subtraction, or you can use the function explode to separate a string.

 

$time = '8 am';
$military_time = expode(' ', $time);

 

Now all you do is.

 

if ($military_time[1] == 'pm')
{
  $newtime = $military_time[0] + 12;
}
else
{
  $newtime = $military_time[0];
}

<?php

  $cst="8am"; // Clock Start Time

  $cet="3pm"; // Clock End Time

  $start = strtotime($cst); // Convert to unix timestamp

  $end = strtotime($cet);

 

  $end+=($end<$start) ?(60*60*24):0; // add 1 day if $end is < $start

  $timespan=(($end-$start)/60)/60;

  echo "$cst - $cet = $timespan hrs.";

?>

 

ok...I guess the topic title is a bit misleading. You see I don't just want to output hours in a timespan....I want to output how many hours are within a given timespan in a timespan. It's hard to explain but if you watch my example carefully maybe you'll see:

 

I have a start-time: eg. 7am

 

I have a end-time : eg. 3pm

 

how many hours in the timespan above are between 12pm and 4pm? the answer is 3 hours(12pm-3pm) in this case...see?

 

now how can I calculate this in php?

if u followed the code givin

and use if statements to limit hrs, it's not hard

<?php

  $tcst="7am"; // Timespan Clock Start Time

  $tcet="3pm"; // Timespan Clock End Time

  $tstart = strtotime($tcst); // Convert to unix timestamp

  $tend = strtotime($tcet);

  $cst="12pm"; // Clock Start Time

  $cet="4pm"; // Clock End Time

  $start = strtotime($cst); // Convert to unix timestamp

  $end = strtotime($cet);

 

 

  $end+=($end<$start) ?(60*60*24):0; // add 1 day if $end is < $start

  $tend+=($tend<$tstart) ?(60*60*24):0; // add 1 day if $tend is < $start for timespan

 

  $start=($start<$tstart)?$tstart:$start; // change start time to timespan if earliear

  $end=($end>$tend)?$tend:$send; // change end time to timespan if later

 

  $timespan=(($end-$start)/60)/60;

  echo "$cst - $cet = $timespan hrs.";

?>

  • 1 month later...

if u followed the code givin

and use if statements to limit hrs, it's not hard

<?php

  $tcst="7am"; // Timespan Clock Start Time

  $tcet="3pm"; // Timespan Clock End Time

  $tstart = strtotime($tcst); // Convert to unix timestamp

  $tend = strtotime($tcet);

  $cst="12pm"; // Clock Start Time

  $cet="4pm"; // Clock End Time

  $start = strtotime($cst); // Convert to unix timestamp

  $end = strtotime($cet);

 

 

  $end+=($end<$start) ?(60*60*24):0; // add 1 day if $end is < $start

  $tend+=($tend<$tstart) ?(60*60*24):0; // add 1 day if $tend is < $start for timespan

 

  $start=($start<$tstart)?$tstart:$start; // change start time to timespan if earliear

  $end=($end>$tend)?$tend:$send; // change end time to timespan if later

 

  $timespan=(($end-$start)/60)/60;

  echo "$cst - $cet = $timespan hrs.";

?>

 

It's been a while, but i tried your code and it worked fine but when the "Timespan Clock Start Time" and "Timespan Clock End Time" were the same as  "Clock Start Time" and "Clock End Time" i got thousands of hours, when it should have been 4.

Archived

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

×
×
  • Create New...

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.