PHP Reading CSV and filter by date

I have the following CSV

Date,Event,Description 24/01/2010,Football,Football practice for all Years. 24/01/2010,Cricket,Cricket Practice for all Years. 25/01/2010,Piano Lessons,Paino lessons for Year 10. 

Date, Event and Description are the headers I want to filter.

I get today's date, and then I want to read the CSV and output the events / events corresponding to the current date. I also want to receive events from tomorrowrows.

+1
source share
6 answers

As mentioned above, you want to use fgetcsv() . Here is how you can do this (untested):

 <?php if (($handle = fopen("test.csv", "r")) !== FALSE) { $today = strtotime("today"); $tomorrow = strtotime("tomorrow"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { if (strtotime($data[0]) == $today || strtotime($data[0]) == $tomorrow)) { echo 'Category: ' . $data[1] . "<br>\n"; echo 'Event: ' . $data[0] . "<br><br>\n\n"; } } fclose($handle); } ?> 
+3
source

The fastest (but not optimal) way to do this is fgetcsv , and then iterate over the table, choosing these dates that are today.

I would probably revise the data format (make it a database) if there are no other applications in it.

+1
source

There are many ways to read CSV files; you can also read line by line and use the split function to split each file as an element in an array.

If search speed and complexity are not a problem, you can perform a linear iteration and check the dates. You can use strtotime to convert the date to a unix timestamp for comparison.

If search speed is important, then convert the date to a timestamp, have an associative array whose key is a timestamp (essentially a hash table)

To get the date tomorrow, check out the documentation on date functions in the PHP manual .

0
source

one way

 $today=date('d/m/Y'); $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y")); $tmr = date("d/m/Y", $tomorrow)."\n"; $handle = fopen("file", "r"); fgets($handle,2048); while (($data = fgetcsv($handle, 2048, ",")) !== FALSE) { $date=explode("/",$data[0]); if ( $today == $data[0] || $tmr == $data[0]){ $j = implode(",", $data); print $j."\n"; } } fclose($handle); 
0
source

As an alternative to fgetcsv and iteration, you can also use regular expressions to get matching strings, for example. for

 Date,Event,Description 24/01/2010,Football,Football practice for all Years. 24/01/2010,Cricket,Cricket Practice for all Years. 25/01/2010,Piano Lessons,Paino lessons for Year 10. 26/01/2010,Piano Lessons II. 

using

 date_default_timezone_set('Europe/Berlin'); $pattern = sprintf('#%s.*|%s.*#', date('d/m/Y'), date('d/m/Y', strtotime("+1 day")) ); $file = file_get_contents('filename.csv'); preg_match_all($pattern, $file, $matches); var_dump($matches); 

and get

 array(1) { [0]=> array(3) { [0]=> string(53) "24/01/2010,Football,Football practice for all Years." [1]=> string(51) "24/01/2010,Cricket,Cricket Practice for all Years." [2]=> string(52) "25/01/2010,Piano Lessons,Paino lessons for Year 10." } } 

Do not compare it. Depending on the size of your CSV file, this can lead to heavy memory due to file_get_contents loading the entire file into a variable.


Another alternative to SplFileObject :

 $today = date('d/m/Y'); $tomorrow = date('d/m/Y', strtotime("+1 day")); $file = new SplFileObject("csvfile.csv"); $file->setFlags(SplFileObject::READ_CSV); foreach ($file as $row) { list($date, $event, $description) = $row; if($date === $today || $date === $tomorrow) { echo "Come visit us at $date for $description"; } } 
0
source

You can spend a lot of time going through each line in the csv file, or you can try something like this:

 $command = sprintf("grep '%s' -Er %s", date('d/m/Y'), $this->db); $result = `$command`; 

This example really works and it is very fast!

0
source

All Articles