zq29 Posted February 12, 2007 Share Posted February 12, 2007 Hello guys and girls, I'm looking for a bit of help with some math / logic more than anything. So here's a bit of background on what I'm doing... I'm writing a small booking system where the user can select a start/end date and time, although the time slots are predefined by radio buttons. There are three start and end time options, for example - Start: 09:30, 12:30, 19:30; End: 11:30, 14:30, 23:30. Now I'm trying to work out how many 'time slots' a user has selected, a single slot being 09:30 - 11:30, two slots being 09:30 - 14:30, and so on. I started out converting the date/time string to a unix timestamp then subtracting one from the other and dividing by 86400 to work out how many whole days had been selected and divided by three to get the amount of slots selected, this started getting messy quite quickly and doesn't really work when selecting start and end dates mid-date. Any ideas on how I can go about this? Feel free to demonstrate your theory with pseudo code, or PHP code if it's easier. I'm crap at maths and this kinda thing takes me hours to figure out - Am I even on the right tracks so far? Many thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/38198-solved-time-slots-help-with-mathlogic-rather-than-code/ Share on other sites More sharing options...
steviewdr Posted February 12, 2007 Share Posted February 12, 2007 So you just want to calculate the time difference between two dates?? So you want to subtract Start Time from End Time, and to return a round number? I dont have time atm to code it. A quick google reveals: http://www.devnewz.com/devnewz-3-20051128CalculatingtheDifferenceBetweenTwoDatesUsingPHP.html -steve Quote Link to comment https://forums.phpfreaks.com/topic/38198-solved-time-slots-help-with-mathlogic-rather-than-code/#findComment-182916 Share on other sites More sharing options...
zq29 Posted February 12, 2007 Author Share Posted February 12, 2007 No, say I select 15/02/2007 09:30 as the start time and 17/02/2007 14:30 as the end time, I need my script to work out that the user wishes to book 8 time slots, so I need the math/process/function to return 8 from the inputs 15/02/2007 09:30 and 17/02/2007 14:30 Quote Link to comment https://forums.phpfreaks.com/topic/38198-solved-time-slots-help-with-mathlogic-rather-than-code/#findComment-182939 Share on other sites More sharing options...
ober Posted February 12, 2007 Share Posted February 12, 2007 Ok, I'm confused between your first and last post. I need some clarification. If you have x number of start times, is each of those start times indicating the beginning of 1 time slot? Or can you have more than one time slot per start/stop time? If it's the first, and you're storing them in a DB: slotid | starttime | stoptime | etc Then you just run some SQL: SELECT COUNT(slotid) FROM slots WHERE starttime >= starttime_selected AND stoptime <= stoptime_selected If it's the second and your calculating slots between points A and B, it gets a little more tricky. So let me know if you need that worked out or explain what's wrong from my understanding above. Quote Link to comment https://forums.phpfreaks.com/topic/38198-solved-time-slots-help-with-mathlogic-rather-than-code/#findComment-182958 Share on other sites More sharing options...
zq29 Posted February 12, 2007 Author Share Posted February 12, 2007 If it's the second and your calculating slots between points A and B, it gets a little more tricky. So let me know if you need that worked out or explain what's wrong from my understanding above. Yes, I'm trying to calculate the number of slots selected between date/time A and B. Apologies for the confusion. Quote Link to comment https://forums.phpfreaks.com/topic/38198-solved-time-slots-help-with-mathlogic-rather-than-code/#findComment-182965 Share on other sites More sharing options...
ober Posted February 12, 2007 Share Posted February 12, 2007 Alright... well that introduces a bunch of other problems. 1) Are we working with an 8 hour day or are these time slots on a 24 hour basis? 2) Are the time slots simply split up evenly between the two points or are they allotted based on a certain number of hours? 3) If they are back to back, is there a break in between? Details man, details. Quote Link to comment https://forums.phpfreaks.com/topic/38198-solved-time-slots-help-with-mathlogic-rather-than-code/#findComment-182977 Share on other sites More sharing options...
zq29 Posted February 12, 2007 Author Share Posted February 12, 2007 I've just figured it out, now this probably isn't the most efficient way to go about this, but it works - Reading my code will probably explain my initial problem a bit better. Thanks for sticking around though ober <?php function return_slots($start,$end) { $c = 0; $s = strtotime($start); $e = strtotime($end); $et = array("11:30","14:30","23:30"); for($t=$s; $t<=$e; $t+=3600) if(in_array(date("H:i",$t),$et)) $c++; return $c." slots"; } echo return_slots("2007-02-15 12:30:00","2007-02-17 11:30:00"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/38198-solved-time-slots-help-with-mathlogic-rather-than-code/#findComment-183063 Share on other sites More sharing options...
ober Posted February 13, 2007 Share Posted February 13, 2007 Without really digging into it to figure out a better way, I'd say that's pretty efficient. Quote Link to comment https://forums.phpfreaks.com/topic/38198-solved-time-slots-help-with-mathlogic-rather-than-code/#findComment-183238 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.