Zaynt Posted February 7, 2008 Share Posted February 7, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/89840-find-how-many-hours-in-a-given-timespan/ Share on other sites More sharing options...
mindspin311 Posted February 7, 2008 Share Posted February 7, 2008 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]; } Quote Link to comment https://forums.phpfreaks.com/topic/89840-find-how-many-hours-in-a-given-timespan/#findComment-460406 Share on other sites More sharing options...
mindspin311 Posted February 7, 2008 Share Posted February 7, 2008 Oh.. and also you want to check if start time is greater than stop time. If it is, then stop time is the next day, and we just add another 24 to stop time, then subtract. Sorry.. it won't let me edit my post for some reason. Quote Link to comment https://forums.phpfreaks.com/topic/89840-find-how-many-hours-in-a-given-timespan/#findComment-460408 Share on other sites More sharing options...
Zaynt Posted February 7, 2008 Author Share Posted February 7, 2008 actually I have the starting time and end-time in unix timestamp format...what would the code look like then ? my timestamp both start and stop time is in this format: mktime(h,m,s ,month, day,year); Quote Link to comment https://forums.phpfreaks.com/topic/89840-find-how-many-hours-in-a-given-timespan/#findComment-460414 Share on other sites More sharing options...
laffin Posted February 7, 2008 Share Posted February 7, 2008 <?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."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/89840-find-how-many-hours-in-a-given-timespan/#findComment-460416 Share on other sites More sharing options...
Zaynt Posted February 7, 2008 Author Share Posted February 7, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/89840-find-how-many-hours-in-a-given-timespan/#findComment-460428 Share on other sites More sharing options...
laffin Posted February 7, 2008 Share Posted February 7, 2008 the timespan between 12pm and 4pm is 4 hrs 12-1 1-2 2-3 3-4 how do u get 3 hrs? Quote Link to comment https://forums.phpfreaks.com/topic/89840-find-how-many-hours-in-a-given-timespan/#findComment-460438 Share on other sites More sharing options...
Zaynt Posted February 7, 2008 Author Share Posted February 7, 2008 only 3 hours of the timespan 7am - 3pm is within the timespan 12pm - 4pm....see? Quote Link to comment https://forums.phpfreaks.com/topic/89840-find-how-many-hours-in-a-given-timespan/#findComment-460440 Share on other sites More sharing options...
laffin Posted February 7, 2008 Share Posted February 7, 2008 than add some if checking to start and end times (using unix timestamps) adjusting the start and end times Quote Link to comment https://forums.phpfreaks.com/topic/89840-find-how-many-hours-in-a-given-timespan/#findComment-460446 Share on other sites More sharing options...
Zaynt Posted February 7, 2008 Author Share Posted February 7, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/89840-find-how-many-hours-in-a-given-timespan/#findComment-460460 Share on other sites More sharing options...
laffin Posted February 7, 2008 Share Posted February 7, 2008 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."; ?> Quote Link to comment https://forums.phpfreaks.com/topic/89840-find-how-many-hours-in-a-given-timespan/#findComment-460480 Share on other sites More sharing options...
haku Posted February 7, 2008 Share Posted February 7, 2008 laffin you've been around enough to know to use code tags around your code. Quote Link to comment https://forums.phpfreaks.com/topic/89840-find-how-many-hours-in-a-given-timespan/#findComment-460584 Share on other sites More sharing options...
Zaynt Posted March 11, 2008 Author Share Posted March 11, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/89840-find-how-many-hours-in-a-given-timespan/#findComment-489841 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.