dubc07 Posted July 24, 2008 Share Posted July 24, 2008 I'm wanting to learn how to calculate time between a start time and an end time in 30 minute increments . $starttime = 600; /////which is militairy time for 6am /////////////// Wanting to caculate the time between the start and the end $endtime = 1400; ///////which is militairy time for 2pm so the total time from start to end would be 8 hours but i would like the output in minutes, which would be 480. Is there any way of doing this and HOW. Thanks. Quote Link to comment Share on other sites More sharing options...
Jabop Posted July 24, 2008 Share Posted July 24, 2008 <?php $StartTime=time(); $TimeElapsed=time()-$StartTime; // do your date conversions here ?> I'm not familiar with converting to military time, but do some research http://us3.php.net/manual/en/function.date.php Quote Link to comment Share on other sites More sharing options...
JonnyThunder Posted July 24, 2008 Share Posted July 24, 2008 Wrote a function to do what you need.... <?php // Example $starttime = 600; /////which is militairy time for 6am $endtime = 1400; ///////which is militairy time for 2pm print give_minutes ($starttime, $endtime); function give_minutes ($starttime, $endtime) { // Get hours / mins for starttime $s_hrs = (int) substr(substr("0".$starttime, -4), 0, 2); $s_mins = (int) substr($starttime, -2); // Get hours / mins for endtime $e_hrs = (int) substr(substr("0".$endtime, -4), 0, 2); $e_mins = (int) substr($endtime, -2); // Make the time into timestamps $i_starttime = mktime($s_hrs, $s_mins, 0); $i_endtime = mktime($e_hrs, $e_mins, 0); // Return it return ($i_endtime - $i_starttime) / 60; } ?> Quote Link to comment Share on other sites More sharing options...
Jabop Posted July 24, 2008 Share Posted July 24, 2008 Not bad, JohnnyThunder. What did you typecast those few vars for? Quote Link to comment Share on other sites More sharing options...
dubc07 Posted July 24, 2008 Author Share Posted July 24, 2008 That will do it. BIG UP's To Jonny Thunder. Thanks Man Quote Link to comment Share on other sites More sharing options...
JonnyThunder Posted July 24, 2008 Share Posted July 24, 2008 Not bad, JohnnyThunder. What did you typecast those few vars for? Well, I didn't really need to - it's just habit i've got into for 'completeness' sake. I think it stems from hack attacks on my websites - typecast everything 'just in case'. Though you're right - PHP would have converted the "00" into a zero anyway. Quote Link to comment Share on other sites More sharing options...
Jabop Posted July 24, 2008 Share Posted July 24, 2008 Well, that's always good practice. Not insulting, just inquiring. But yeah, PHP does coercion or whatever it is. Quote Link to comment Share on other sites More sharing options...
Jabop Posted July 24, 2008 Share Posted July 24, 2008 Now that I actually mess around with the function, I am also curious as to why you nested two substrings in the $s_hrs and $e_hrs, when the same can be accomplished with substr($starttime, 0, 2); Quote Link to comment Share on other sites More sharing options...
JonnyThunder Posted July 24, 2008 Share Posted July 24, 2008 The same cannot be accomplished with what you said. because that value can be one or two digits. I used two substrs, because I needed to split the hours from the minutes. And because the hours can be one or two digits - I prefixed it with a "0". Thats the first substring. At this stage, I know the overall size of the time will be 4 characters. The second substr, was to split the minutes from the hours. Of course, the minutes will always be two digits according to military time. So I can safely assume it'll always be the last 2 characters of the string. Quote Link to comment Share on other sites More sharing options...
Jabop Posted July 24, 2008 Share Posted July 24, 2008 I believe I was initially confused because I don't understand military time enough to just read it as I see it. So, I thought since the end time was 1400, that, it would be 01400 with your substring, then the string returned would have been 01 and not 14 like it should have been. Quote Link to comment Share on other sites More sharing options...
JonnyThunder Posted July 24, 2008 Share Posted July 24, 2008 It would return 01400 - but i'm grabbing the last four characters of the string (using negative value in the substr command) - which I know will always be good, whether the original is four or five characters. Like this.... If they put in 600 as the value, then im doing this... 0600 then taking the LAST four characters, which will be 0600. if however, they put in this 1400 to start, then im doing this... 01400 And still grabbing the last four characters which will be 1400. it's kindof a way of doing it blind - without knowing what the first characters will be. (I do however, have to assume 2 characters for the minutes for this to work) Quote Link to comment Share on other sites More sharing options...
Jabop Posted July 24, 2008 Share Posted July 24, 2008 /facepalm to myself I get it now, the only confusion I had is because I'm not versed with military times so it made it look confusing. Forgive me haha Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 24, 2008 Share Posted July 24, 2008 just so we're all clear, with military time, the function is extreme overkill: $difference = $endtime - $starttime; $modulo = $difference % 100; $minutes = (($difference - $modulo) * 0.6) + $modulo; then do what you want with the $minutes. this is all just math - there should be no string manipulation necessary. the second line grabs the remainder after dividing by 100 (in the case that the times are not dead on an hour), the third line calculates the number of minutes by multiplying the hours by minutes, and adding the remainder. the reason for 0.6 is that you multiply by 60 (for 60 minutes in one hour) and you divide by 100 (since military time designates the hour in hundreds). 60/100 = 0.6. jonny's method probably works, and should be given credit. i just think it's good practice not to bother using string manipulation unless you're manipulating strings. Quote Link to comment Share on other sites More sharing options...
JonnyThunder Posted July 24, 2008 Share Posted July 24, 2008 That'd explain why I never did it that way. I've always been crap at maths! I'm never posting again. Quote Link to comment Share on other sites More sharing options...
akitchin Posted July 24, 2008 Share Posted July 24, 2008 don't stop posting, you gave him a solution that works. i just felt like pointing out the mathematical method . Quote Link to comment Share on other sites More sharing options...
JonnyThunder Posted July 24, 2008 Share Posted July 24, 2008 Nah kudos mate... i'm impressed. (Just gonna wait till you're asleep before I post!) Quote Link to comment Share on other sites More sharing options...
Jabop Posted July 24, 2008 Share Posted July 24, 2008 Not being good at math sucks, I know all too well. Quote Link to comment Share on other sites More sharing options...
JonnyThunder Posted July 24, 2008 Share Posted July 24, 2008 Viva la string manipulation!! Long live processor overkill! Quote Link to comment 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.