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/201
As an alternative to fgetcsv
and iterating, you could also use a Regular Expressions to get the appropriate lines, e.g. 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.
use
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 receive
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."
}
}
Haven't benchmarked this though. Depending on the size of your CSV file, this could get memory intensive, due to file_get_contents
loading the entire file into a variable.
Another alternative with 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";
}
}