I am trying to order by date DESC some events read from a CSV and print out them.
The CSV file has to be read until it reaches the end and then order by date DESC only the entries in the following categories: champions and euro.
This is the code I wrote but I cannot order the results. Any help would be very appreciated, because I am really stucked on this.
$today = date('Y-m-d', strtotime("now"));
$header = fgetcsv($handle);
$length = 1000;
$delimiter = ";";
$date = array();
$rows = array();
while ( ( $row = fgetcsv($handle, $length, $delimiter) ) != FALSE) {
if( ($row[2] == "champions" || $row[2] == "euro") ) {
// if there is a , in the date take the first part of it
preg_match('~([^\s]+)(?:,.*)?$~', $row[3], $m); // This is probably wrong (preg_match drives me crazy)
$date[] = $m[1];
$rows[] = $row;
}
}
fclose($handle);
array_multisort($date, $rows);
foreach ($rows as $entry) {
$csv_date = date('Y-m-d', strtotime($entry[3]));
if($csv_date >= $today) {
if( ($i == 0 || $i <= 2) ) {
echo date('M. ' . 'Y', strtotime($entry[3])) . "<br/>";
echo date('d', strtotime($entry[3])) . "<br/>";
echo "<br/>";
// Print only 3 matches
echo $entry[8];
$i++;
}
}
}