View Full Version : time table

04-02-2007, 09:48 AM
hi everyone,

can someone help me out with this script i'm making?

basicly i have a bus timetable, and i want the script to show the 2 nearest buses and if I enter an hour, that it would also show the 2nearest buses for that inputed time.

i've tried using sscanf and other strings, but couldn't get anything done!.. :(

04-02-2007, 03:20 PM
Well, first of all, you need to show us some scripting you've done so far.

Secondly, this sounds like a homework assignment, and we don't
do people's homework.

If this is a real website PHP project, give us a link to the site, and also
some scripting you've done so far ... the script that isn't working.

04-02-2007, 04:37 PM
as I said, nothing worked for me. do you want me to give you useless code that only shows errors?

this is not homework for Pete's sake. here Sherlock: http://tv.virusas.org

04-02-2007, 04:45 PM
as I said, nothing worked for me. do you want me to give you useless code that only shows errors?

Yep, then we can help you sort your code out, tell you where you went wrong, etc.

04-02-2007, 04:53 PM
here's something I'm looking for: http://www.cems.uwe.ac.uk/~cjwallac/apps/bus/serviceboard.php%3fstyle=screen&refresh=60

I simply deleted it because it was totally useless, that's why I'm asking for help.

04-02-2007, 05:07 PM
Man I had this great post with code to help you, but... I deleted it. Sorry :(

04-02-2007, 05:19 PM
I look more like Dr. Watson ...

But, anyhow, can we see the database or text
that you're reading for the bus timetable?

04-02-2007, 05:25 PM
This is why I asked if it was homework/school related:


It just seems like a school project for a programming class ...

-Dr. Watson-

04-02-2007, 05:26 PM

well there are different bus times from monday to friday, saturday and sunday. but so far I only need monday to friday.

Hour Minutes

5 28 44
6 00 18 38 56
7 14 33 55
8 19 46
9 29
10 41
11 18
12 09 46
13 37
14 19 44
15 10 28 48
16 06 26 44 56
17 16 36 58
18 18 34
19 08 15 37
20 13 50
21 38
22 16
23 03 38
0 25

04-02-2007, 05:37 PM
OK, I'm done being a smart-alec (I'm just in a punchy mood today) ...

seriously now ...

If you have an actual web page that shows the bus timetable,
you can use PHP to open that page, read the timetable and display
the closest buses.

That way, if the timetable changes, you would always be looking at
the latest schedule.

This would be similar to an RSS Feeder (using PHP scripting). Would
something like this be more valuable to you? Let us know and provide
a URL to a timetable. Even a timetable with every day of the week,
not just Monday to Friday.

04-02-2007, 06:15 PM
well how do I do that?

04-02-2007, 06:50 PM
here you go:

04-02-2007, 06:51 PM
I suggest picking up a php book or searching for tutorials. Were not gonna write your code for you, especially when it seems you don't even know what php is.

EDIT: This is a php forum not a asp forum. You trying to trick us into thinking you did that one? Come on... I still think its homework and he didnt pay attention in class LOL!

04-02-2007, 06:55 PM
if you're so smart then give me a link to a tut, cuz i couldn't find one. maybe einstein here has a tut?

04-02-2007, 06:55 PM
give us a URL to a page with a bus timetable.

Every HTML page that is parsed is slightly different,
so there is no "universal" PHP script.

But this is a simplified example of what happens.
1) Opens the URL, grabs all data between <html> and </html>
2) Parses out what is needed between various tags.
3) Process the info
4) Display results


// Get page
$url = "http://www.google.com";
$data = implode("", file($url));

// Get Page Content
preg_match_all ("/<html>([^`]*?)<\/html>/", $data, $matches);

// Loop through each item
foreach ($matches[0] as $match) {
// Get title
preg_match ("/<title>([^`]*?)<\title>/", $match, $temp);
$title = $temp['1'];
$title = strip_tags($title);
$title = trim($title);

// process the HTML, grabbing information
// and doing calculations for time, etc.

// Echo Items
echo "Website Title: $title\n";

You would open your URL and begin parsing out times ...
Looking at the URL you provided, the times are really mashed into <table> tags.
I don't know the website's language, so I can't decipher what the times mean.
But, the tiny example above is the approach you would take.

04-02-2007, 06:58 PM
I'm not that stupid :) I just can't get it to pick the closest 2 buses and show them

04-02-2007, 07:06 PM
So your trying to pull the buses from someone else's website? Am I understanding that correctly?

04-02-2007, 07:13 PM
In CodilX's defense, the timetables that they have provided are
horrible. I don't blame him for trying to parse it out so it can be
easily deciphered.

I just see now that parsing that horrible HTML is going to be
a big job. He might be better-off emailing the bus company and
pleading with them to provide a better customer interface.

But for learning PHP, it would be a good project.

I gave him a starting point ... that's all I have time to contribute.

04-02-2007, 07:15 PM
what the ?.. are you really that stupid? why the hell would I want to pull buses out of 'someones' website?

these are the traffic schedules of a damn public transport system.

check it out smartass http://www.vilniustransport.lt/en/

04-02-2007, 07:21 PM
LOL ok I'm really confused, what exactly are you trying to do? So you own that website or what?

LMAO mlseim, do you know what hes doing? Enlighten me some :)

04-02-2007, 07:21 PM
mlseim: since you're probably the most normal of all ppl here, what do you suggest? the timetables change only like once a year or smth, I could easily save it to a file or have it in the .php file. I just need help with getting the 2 nearest buses

04-02-2007, 07:24 PM
They're trying to guess where you're getting the timetable info from. You're not helping at all as you're not answering their questions. You're throwin up links to sites, but not saying why.

Are you getting info from your database, from a flat file, from an array, from another site, from a magic hat?

No one can help if you don't explain

04-02-2007, 07:26 PM
Im sorry i don't have an answer and this is really off topic but if you read the whole thing through its funny. Ha He Ha Ha!

04-02-2007, 07:29 PM
I think he wants to grab the latest busses arriving now, or at any time
specified. But (and I'm guessing at this) ... the website he provided is
really difficult to figure out, so if there was a PHP script that simply showed
the next two busses in an easy-to-see way ... that's what he is going to
create using PHP.

It's not in English, but looking at the bus timetables, it's really confusing.
In America, where we don't have a decent bus system, I guess that I just
don't know what the timetables mean.

It's not uncommon to parse information from someone else's website.
That's the basic premise behind RSS, but in this case, that public transport
company doesn't provide an RSS Feed (they should though).

When he parses their HTML and uses that information on his site, or
wherever, a simple link back would be ethical. Again, the same as RSS.

Looking at the convoluted HTML in the timetable pages, this is going to be
one big parsing headache. At first, it did look like homework to me, but now
I think I see what he is trying to do.

CodilX ... how close did I come to understanding?

04-02-2007, 07:47 PM
file_get_contents works too :)

04-02-2007, 08:11 PM
I reedited the link to the english version

iLLin: file_get_contents works gr8 for me too, but if you're so smart then you can help out with the bus times?

Nightfire: uhh.. is it that difficult to understand that my source is the webpage that I posted?

mlseim: bullseye. but as I said, I don't have to parse it from that page every time, I could easily just set the times as variables or a .txt file

the-dream: heheheh

04-02-2007, 08:20 PM
Ok see now I understand. Now to get more specific you want to pull the 2 closest buses with what criteria? Pending on the users location? Your location? A location you enter? The time of day?

04-02-2007, 08:26 PM
CodilX ...

Here's the data file, called "timetable.txt"

bus-A 5 28 44
bus-A 6 00 18 38 56
bus-A 7 14 33 55
bus-A 8 19 46
bus-A 9 29
bus-A 10 41
bus-A 11 18
bus-A 12 09 46
bus-A 13 37
bus-A 14 19 44
bus-A 15 10 28 48
bus-A 16 06 26 44 56
bus-A 17 16 36 58
bus-A 18 18 34
bus-A 19 08 15 37
bus-A 20 13 50
bus-A 21 38
bus-A 22 16
bus-A 23 03 38
bus-A 0 25

Here's the PHP script called: timetable.php


#Read Timetable File
$fp = fopen($filename, "r");
$array1 = explode("\n", fread($fp, filesize($filename)));
fclose ($fp);

$current_hour=date('G'); // 24hr without leading zero
$current_minute=date('i'); // minute with leading zero

echo "Current Time: $current_hour:$current_minute <br><br> \n";
echo "The next two buses are: <br> \n";

for ($i=0;$i<$count_array;$i++) {
list ($bus, $hour, $min1, $min2, $min3, $min4, $min5) = split (' ', $array1[$i]);

if(($current_hour <= $hour)&&($bus1 < 2)){
if($current_minute <= $min1){echo "$bus $hour:$min1 <br> \n"; $bus1++;}
if($current_minute <= $min2){echo "$bus $hour:$min2 <br> \n"; $bus1++;}
if($current_minute <= $min3){echo "$bus $hour:$min3 <br> \n"; $bus1++;}
if($current_minute <= $min4){echo "$bus $hour:$min4 <br> \n"; $bus1++;}
if($current_minute <= $min5){echo "$bus $hour:$min5 <br> \n"; $bus1++;}


if($bus1 == 0){ // no buses found - too late in the day? reset and pick again
for ($i=0;$i<$count_array;$i++) {
list ($bus, $hour, $min1, $min2, $min3, $min4, $min5) = split (' ', $array1[$i]);

if(($current_hour <= $hour)&&($bus1 < 2)){
if($current_minute <= $min1){echo "$bus $hour:$min1 <br> \n"; $bus1++;}
if($current_minute <= $min2){echo "$bus $hour:$min2 <br> \n"; $bus1++;}
if($current_minute <= $min3){echo "$bus $hour:$min3 <br> \n"; $bus1++;}
if($current_minute <= $min4){echo "$bus $hour:$min4 <br> \n"; $bus1++;}
if($current_minute <= $min5){echo "$bus $hour:$min5 <br> \n"; $bus1++;}




Here's the working test script (using my server's current time) ...

A possible problem ...
Your server times may be wrong (the current time).

You'll have to adjust $current_hour if it's wrong.

I didn't put in a variable for "any time", but you have
enough to get the idea.

EDIT: Another problem ... I didn't allow for wrapping
around ... example, hour 23 doesn't look
ahead to hour 0. Some more "if" statements if
nothing is found. There should always be two
buses found, not matter what time it is. SEE SCRIPT (above) FOR CHANGES.

04-02-2007, 08:40 PM
mlseim: my man!!! thank you so much. does everybody see? aah? the best face on codingforums!

04-02-2007, 09:05 PM
I would've stopped giving help after the first insult.

For what reason would you insult those trying to help you out? No matter whether it is helpful or not, they're not being paid to do this.


04-02-2007, 09:05 PM
Well ...

Now I see a couple of more bugs in my script.

I think you'll need to tweak it a bit.
But there's enough there to get an idea of how it works.

04-03-2007, 05:18 AM
CodilX, if you can not get your point across over without being rude then please do not post, if you do this again the choice will no longer be yours, closing thread.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum