0

I'm new to PHP and have I problem right now. I want to make a file with bus times for a day, and want to show a user the nearest bus in the future. Every day there are just 15 buses, so it wouldnt be a big file. I thought about saving the file like this: 0120, 0230, 1020,1340 and so on. MY thinking right now is to read from this file by fgetcsv with a for loop and store all times in an array which are > than date("His"). This would happen in the controller. In my file which shows the output to the user I would then show the first element of the array in big font as the next bus and the next 10 elements in a smaller font. Would this work? I am not sure with my time format. Can I compare 0230 from a text file with the output from date()?

Thanks for your help!

Edit: Based on the comments below I tried this. Based on my understanding this should read in one element of the csv every iteration of the while loop, right?

<?php
$buses = 0;
$hoursMins = date("Gi");
$times = [];
if (($handle = fopen("1.csv", "r")) !== FALSE) 
{
    while (($element = fgetcsv($handle, 200, ",")) !== FALSE) 
    {
        if($hoursMins < $element)
        {
            $times = $element;
            echo $times[$buses++] . "<br />\n";
            $buses++;
        }

    }
    fclose($handle);
}
?>

This is only showing me 755 though.

rStorms
  • 1,036
  • 2
  • 11
  • 23

2 Answers2

1

I would suggest to instead use date("Gi"). The "G" represents 24-hour format of an hour without leading zeros. The leading zero can cause a lot of weird issues since they are considered base8 numeric values:

var_dump(0200); // int(128)

Although date() and fgetcsv() will return strings, NOT doing zero-padded strings can only save you from possible issues if you ever cast those values as a numeric type.

bparise
  • 3,809
  • 2
  • 13
  • 5
  • Thanks! I edited my post. As I said Im new to php and am not sure why my code doesnt work as I want it too? Any idea? – rStorms Apr 06 '13 at 18:28
  • +1 @bparise - thanks so much for the catch on the leading zero quirks! I've incorporated your format string into my answer, citing you as the source. – Ed Gibbs Apr 06 '13 at 19:40
0

Yes, you can. To get the current time as hours/minutes in 24-hour format, just use something like this:

$hoursMins = date('Gi');

If you run this statement at, say, 2:06PM you'll get 1406. If you run it at 7:15AM you'll get 715.

Full disclosure: I originally had the format string as Hi but as bparise correctly explains in another answer, Gi is better.


To read a small file like yours, use the file_get_contents function to suck the entire file into a string:

$timesList = file_get_contents('1.csv');

From there, you can "explode" it into an array:

$times = explode(',', $timesList);

Then you can just loop through the array and look for the time that's greater than or equal to the current time. As for the current time, you might be better off with the Gi format in the date call as mentioned in another answer because it won't have a leading zero if the hour is less than 10AM.

Ed Gibbs
  • 25,924
  • 4
  • 46
  • 69
  • Okay thats great, thanks. If I for example have the file: 1.csv being this: 755, 825, 905, 940, 1010, 1040, 1730, 1800, 1830, 1900, 1920, 1940, 2000, 2020, 2100, 2120, 2140, 2200, 2220, 2240, 2300 How can I then read until I am at the end of the file? In c I could check for NULL or !EOF, but not quite sure how to do this in PHP. Thanks again! – rStorms Apr 06 '13 at 17:46
  • You could go through line by line, but with such a small file the `file_get_contents` function is easiest. I've updated my answer to include that. – Ed Gibbs Apr 06 '13 at 18:33
  • Thanks, works great! Did it like this: \n"; $num++; } } echo $num; ?> Could I do this more effective? Would appreciate some tips on my code ;) – rStorms Apr 06 '13 at 19:19
  • That looks good. The only inefficiency I see is that you don't need to build the `$times` array because you're reporting the times as you go. You could remove all references to the `$times` array and just `echo $nextbuses[$i] . "
    \n";` when you find a reportable time. That said, the program will basically run instantaneously the way you have it already, so the only real efficiency you get from removing `$times` is a slight improvement in readability because there's one less array to keep track of. Also, I'm updating my answer above to show `Gi` instead of `Hi` because it's best.
    – Ed Gibbs Apr 06 '13 at 19:33
  • Okay, thanks a lot four your help. Very much appreciated. A small other question. I need to have the timeformat without a : to make the time comparable. However after doing that I would like to add the : back at the third last position of the string. Any idea how that is possible? – rStorms Apr 07 '13 at 11:41
  • Probably the easiest way is with `substr`: `substr($nextbuses[$i], 0, -2) . ':' . substr($nextbuses[$i], -2)` will put the colon in the right place for any of your values. – Ed Gibbs Apr 07 '13 at 17:31