clueless22 Posted January 10, 2009 Share Posted January 10, 2009 I am trying to write a script that will automatically return the next 3 times a bus will arrive at a given stop based on an existing spreadsheet or table. For instance, as I am writing this it is 12:48pm. I want the website to tell me that this particular route will reach my stop again at 12:52, 1:04, and 1:16 (the three consecutive times immediately following the current time). I was able to accomplish this manually by inserting a whole lot of elseif commands, but this is way too much work, since the code below is only for one stop of one bus route - and there are six routes each having up to 20 stops. <?php date_default_timezone_set('America/New_York'); $hours = date(G); $sec_hours = $hours * 3600; $minutes = date(i); $sec_minutes = $minutes * 60; $seconds = date(s); $time = $sec_hours + $sec_minutes + $seconds; echo("<center><span style=\"font-size:18px\"><font face=\"Helvetica\">"); if (($time >= 21600) && ($time < 24180)) { echo("6:42 am<br>7:04 am<br>7:16am"); } elseif (($time >= 24180) && ($time < 25500)) { echo("7:04 am<br>7:16 am<br>7:28 am"); } elseif (($time >= 25500) && ($time < 26220)) { echo("7:16 am<br>7:28 am<br>7:40 am"); } elseif (($time >= 26220) && ($time < 26940)) { echo("7:28 am<br>7:40 am<br>7:52 am"); } elseif (($time >= 26940) && ($time < 27660)) { echo("7:40 am<br>7:52 am<br>8:04 am"); } elseif (($time >= 27660) && ($time < 28380)) { echo("7:52 am<br>8:04 am<br>8:16 am"); } elseif (($time >= 28380) && ($time < 29100)) { echo("8:04 am<br>8:16 am<br>8:28 am"); } elseif (($time >= 29100) && ($time < 29820)) { echo("8:16 am<br>8:28 am<br>8:40 am"); } elseif (($time >= 29820) && ($time < 30540)) { echo("8:28 am<br>8:40 am<br>8:52 am"); } elseif (($time >= 30540) && ($time < 31260)) { echo("8:40 am<br>8:52 am<br>9:04 am"); } elseif (($time >= 31260) && ($time < 31980)) { echo("8:52 am<br>9:04 am<br>9:16 am"); } elseif (($time >= 31980) && ($time < 32700)) { echo("9:04 am<br>9:16 am<br>9:28 am"); } elseif (($time >= 32700) && ($time < 33420)) { echo("9:16 am<br>9:28 am<br>9:40 am"); } elseif (($time >= 33420) && ($time < 34140)) { echo("9:28 am<br>9:40 am<br>9:52 am"); } elseif (($time >= 34140) && ($time < 34860)) { echo("9:40 am<br>9:52 am<br>10:04 am"); } elseif (($time >= 34860) && ($time < 35580)) { echo("9:52 am<br>10:04 am<br>10:16 am"); } elseif (($time >= 35580) && ($time < 36300)) { echo("10:04 am<br>10:16 am<br>10:28 am"); } elseif (($time >= 36300) && ($time < 37020)) { echo("10:16 am<br>10:28 am<br>10:40 am"); } elseif (($time >= 37020) && ($time < 37740)) { echo("10:28 am<br>10:40 am<br>10:52 am"); } elseif (($time >= 37740) && ($time < 38460)) { echo("10:40 am<br>10:52 am<br>11:04 am"); } elseif (($time >= 38460) && ($time < 39180)) { echo("10:52 am<br>11:04 am<br>11:16 am"); } elseif (($time >= 39180) && ($time < 39900)) { echo("11:04 am<br>11:16 am<br>11:28 am"); } elseif (($time >= 39900) && ($time < 40620)) { echo("11:16 am<br>11:28 am<br>11:40 am"); } elseif (($time >= 40620) && ($time < 41340)) { echo("11:28 am<br>11:40 am<br>11:52 am"); } elseif (($time >= 41340) && ($time < 42060)) { echo("11:40 am<br>11:52 am<br>12:04 pm"); } elseif (($time >= 42060) && ($time < 42780)) { echo("11:52 am<br>12:04 pm<br>12:16 pm"); } elseif (($time >= 42780) && ($time < 43500)) { echo("12:04 pm<br>12:16 pm<br>12:28 pm"); } elseif (($time >= 43500) && ($time < 44220)) { echo("12:16 pm<br>12:28 pm<br>12:40 pm"); } elseif (($time >= 44220) && ($time < 44940)) { echo("12:28 pm<br>12:40 pm<br>12:52 pm"); } elseif (($time >= 44940) && ($time < 45660)) { echo("12:40 pm<br>12:52 pm<br>1:04 pm"); } elseif (($time >= 45660) && ($time < 46380)) { echo("12:52 pm<br>1:04 pm<br>1:16 pm"); } elseif (($time >= 46380) && ($time < 47100)) { echo("1:04 pm<br>1:16 pm<br>1:28 pm"); } elseif (($time >= 47100) && ($time < 47820)) { echo("1:16 pm<br>1:28 pm<br>1:40 pm"); } elseif (($time >= 47820) && ($time < 48540)) { echo("1:28 pm<br>1:40 pm<br>1:52 pm"); } elseif (($time >= 48540) && ($time < 49260)) { echo("1:40 pm<br>1:52 pm<br>2:04 pm"); } elseif (($time >= 49260) && ($time < 49980)) { echo("1:52 pm<br>2:04 pm<br>2:16 pm"); } elseif (($time >= 49980) && ($time < 50700)) { echo("2:04 pm<br>2:16 pm<br>2:28 pm"); } elseif (($time >= 50700) && ($time < 51420)) { echo("2:16 pm<br>2:28 pm<br>2:40 pm"); } elseif (($time >= 51420) && ($time < 52140)) { echo("2:28 pm<br>2:40 pm<br>2:52 pm"); } elseif (($time >= 52140) && ($time < 52860)) { echo("2:40 pm<br>2:52 pm<br>3:04 pm"); } elseif (($time >= 52860) && ($time < 53580)) { echo("2:52 pm<br>3:04 pm<br>3:16 pm"); } elseif (($time >= 53580) && ($time < 54300)) { echo("3:04 pm<br>3:16 pm<br>3:28 pm"); } elseif (($time >= 54300) && ($time < 55020)) { echo("3:16 pm<br>3:28 pm<br>3:40 pm"); } elseif (($time >= 55020) && ($time < 55740)) { echo("3:28 pm<br>3:40 pm<br>3:52 pm"); } elseif (($time >= 55740) && ($time < 56460)) { echo("3:40 pm<br>3:52 pm<br>4:04 pm"); } elseif (($time >= 56460) && ($time < 57180)) { echo("3:52 pm<br>4:04 pm<br>4:16 pm"); } elseif (($time >= 57180) && ($time < 57900)) { echo("4:04 pm<br>4:16 pm<br>4:28 pm"); } elseif (($time >= 57900) && ($time < 58620)) { echo("4:16 pm<br>4:28 pm<br>4:40 pm"); } elseif (($time >= 58620) && ($time < 59340)) { echo("4:28 pm<br>4:40 pm<br>4:52 pm"); } elseif (($time >= 59340) && ($time < 60060)) { echo("4:40 pm<br>4:52 pm<br>5:04 pm"); } elseif (($time >= 60060) && ($time < 60780)) { echo("4:52 pm<br>5:04 pm<br>5:16 pm"); } elseif (($time >= 60780) && ($time < 61500)) { echo("5:04 pm<br>5:16 pm<br>5:28 pm"); } elseif (($time >= 61500) && ($time < 62220)) { echo("5:16 pm<br>5:28 pm<br>5:40 pm"); } elseif (($time >= 62220) && ($time < 62940)) { echo("5:28 pm<br>5:40 pm<br>5:52 pm"); } elseif (($time >= 62940) && ($time < 63660)) { echo("5:40 pm<br>5:52 pm<br>6:03 pm"); } elseif (($time >= 63660) && ($time < 64380)) { echo("5:52 pm<br>6:03 pm<br>6:19 pm"); } elseif (($time >= 64380) && ($time < 65040)) { echo("6:03 pm<br>6:19 pm<br>6:36 pm"); } elseif (($time >= 65040) && ($time < 66000)) { echo("6:19 pm<br>6:36 pm<br>6:52 pm"); } elseif (($time >= 66000) && ($time < 67020)) { echo("6:36 pm<br>6:52 pm<br>7:09 pm"); } elseif (($time >= 67020) && ($time < 67980)) { echo("6:52 pm<br>7:09 pm<br>7:25 pm"); } elseif (($time >= 67980) && ($time < 69000)) { echo("7:09 pm<br>7:25 pm<br>7:42 pm"); } elseif (($time >= 69000) && ($time < 69960)) { echo("7:25 pm<br>7:42 pm<br>7:58 pm"); } elseif (($time >= 69960) && ($time < 70980)) { echo("7:42 pm<br>7:58 pm<br>8:15 pm"); } elseif (($time >= 70980) && ($time < 71940)) { echo("7:58 pm<br>8:15 pm<br>8:31 pm"); } elseif (($time >= 71940) && ($time < 72960)) { echo("8:15 pm<br>8:31 pm<br>8:48 pm"); } elseif (($time >= 72960) && ($time < 73920)) { echo("8:31 pm<br>8:48 pm<br>9:18 pm"); } elseif (($time >= 73920) && ($time < 74940)) { echo("8:48 pm<br>9:18 pm<br>9:48 pm"); } elseif (($time >= 74940) && ($time < 76740)) { echo("9:18 pm<br>9:48 pm<br>10:18 pm"); } elseif (($time >= 76740) && ($time < 78540)) { echo("9:48 pm<br>10:18 pm<br>10:48 pm"); } elseif (($time >= 78540) && ($time < 80340)) { echo("10:18 pm<br>10:48 pm<br>11:18 pm"); } elseif (($time >= 80340) && ($time < 82140)) { echo("10:48 pm<br>11:18 pm<br>11:48 pm"); } elseif (($time >= 82140) && ($time < 83940)) { echo("11:18 pm<br>11:48 pm"); } elseif (($time >= 83940) && ($time < 85740)) { echo("11:48 pm"); } elseif (($time >= 0) && ($time < 21600)) { echo("Out of service."); } else { echo("Out of service."); } echo("</font></span></center>"); ?> Does anyone have any suggestions of how to make this easier? I have access to but limited knowledge of MySQL if that is required. Any assistance is greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/140298-bus-schedule-script/ Share on other sites More sharing options...
clueless22 Posted January 10, 2009 Author Share Posted January 10, 2009 Please view the script as it exists now in action @ http://www.kchek.com/bus/blue/north.php Link to comment https://forums.phpfreaks.com/topic/140298-bus-schedule-script/#findComment-734118 Share on other sites More sharing options...
clueless22 Posted January 10, 2009 Author Share Posted January 10, 2009 Also, here is the table from which I would like the times to be drawn automatically: http://www.kchek.com/bus/blue/blue.htm The script in my previous posts uses the times in Column 1. Ultimately I would like to have a page for each column, which represent all the bus stops on this route. Link to comment https://forums.phpfreaks.com/topic/140298-bus-schedule-script/#findComment-734125 Share on other sites More sharing options...
Mark Baker Posted January 10, 2009 Share Posted January 10, 2009 You say that the timetable exists in a database or a spreadsheet. How are you reading the data from that? Link to comment https://forums.phpfreaks.com/topic/140298-bus-schedule-script/#findComment-734225 Share on other sites More sharing options...
clueless22 Posted January 10, 2009 Author Share Posted January 10, 2009 I'm not sure I understand your question. The timetable exists here: http://kchek.com/bus/blue/blue.htm (this is just an excel spreadsheet saved as a webpage). Right now, my script and this table are not linked in any way. I manually entered the times from the table into the php file. I am looking for some way to automate that. Link to comment https://forums.phpfreaks.com/topic/140298-bus-schedule-script/#findComment-734229 Share on other sites More sharing options...
Mark Baker Posted January 10, 2009 Share Posted January 10, 2009 Right now, my script and this table are not linked in any way. I manually entered the times from the table into the php file. I am looking for some way to automate that.Well first, you need to be able to access the Excel workbook from within your PHP script; and for that task, I'd point you to the PHPExcel library listed in my signature Link to comment https://forums.phpfreaks.com/topic/140298-bus-schedule-script/#findComment-734231 Share on other sites More sharing options...
clueless22 Posted January 12, 2009 Author Share Posted January 12, 2009 Thanks Mark, but that seems way over my head. I have converted the timepoint schedule to an XML document: http://www.kchek.com/bus/blue.xml I know that XML files can be read using HTML, so hopefully PHP can get the job done as well. However, I still have no idea as to how to tell the browser to return only the relevant values, those being the next 3 consecutive timepoints (based on the current time). Link to comment https://forums.phpfreaks.com/topic/140298-bus-schedule-script/#findComment-735490 Share on other sites More sharing options...
Mark Baker Posted January 12, 2009 Share Posted January 12, 2009 I have converted the timepoint schedule to an XML document: http://www.kchek.com/bus/blue.xml I know that XML files can be read using HTML, so hopefully PHP can get the job done as well. In that case, I'd suggest taking a look at xpath functions in PHP. Looking at your XML, you could make life a lot easier for yourself by using 24-hour clock, because AM and PM complicate sorting your time values. Your original html version did actually use 24-hour clock Link to comment https://forums.phpfreaks.com/topic/140298-bus-schedule-script/#findComment-735623 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.