sprdnja Posted February 22, 2011 Share Posted February 22, 2011 I'm new here so I say hallo to everyone I have to implement bus time table in this way: Every bus drives every second day from the day chosen. Let's say: If xx bus goes every second day from 23rd February to 30th June to Spain (23rd, 25th, 27th...), How can I check if let's say on 14th May there is bus to Spain? I started with odd and even numbers: if it starts from the even day than there is array of months with number of days, but what if Feb has 29days (every 4th year) instead of 28... I'm totally confused here. In database I have rows: start date, end date and bus name. Is there any other way of implementing this? Quote Link to comment Share on other sites More sharing options...
denno020 Posted February 22, 2011 Share Posted February 22, 2011 Welcome to the forum. I'm not sure of the exact coding you would need, but I do know how you could go about getting it done. You would store the date that the bus 'starts' or whatever. Then on any day later than that, use PHP and the date functions to find out how many days between the current day the the 'start' day. You then divide the difference by 2. To make it easy, instead of using '/' as the divide operator, use '%', which will give the remainder as a result. In if tags you then say: if(($days_difference % 2) == 0){ //the bus will run today }else{ //the bus won't run today } Does that make sense? Denno Quote Link to comment Share on other sites More sharing options...
sprdnja Posted February 22, 2011 Author Share Posted February 22, 2011 Denno020, I have to say one thing - you are genius I totally mis-minded that solution and date functions. It is easiest way ever. In one moment I found myself of writing function that already exist in PHP :-) Here is how I done this: 1. first implement calculation between dates using snippet http://snipplr.com/view/2223/get-number-of-days-between-two-dates/ 2. then calculate if the number odd or even, if it is even everything is OK $numberofdaysbetween = count_days($date1, $date2); $bus=($numberofdaysbetween&1) ? "odd" : "even"; echo "$bus"; 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.