Jump to content

Bus schedule/timetable on every 2nd day


sprdnja

Recommended Posts

I'm new here so I say hallo to everyone  :D

 

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

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.