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
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];
}

Link to comment
Share on other sites

<?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.";

?>

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

Can you give me an example? I'm pretty new at this an I don't know where to start with the if-statements..

 

lets use this as a starting point:

 

start-time = 07:00

 

end-time = 15:00

 

how many hours in that timespan are between 12:00 and 16:00?

Link to comment
Share on other sites

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.";

?>

Link to comment
Share on other sites

  • 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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.