-2

I am trying to parse a csv file in php to try and figure out how to get all data between some dates and the sort according to the date in ascending order but I am not able to sort the date column or I have no idea what format it is in please help

    ID       Reg Date    FirstName    LastName
    1        1278336015   Sergio       Roberto
    2        1395656121   Ray          Wilkins
    3        1300276526   Trueman      Ted
    4        1374087492   Volt         John

Please assist thanks

Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58
Lens
  • 3
  • 2
  • the date looks like a timestamp. `date("Y-m-d H:i:s",1278336015);` `echo $x; //2010-07-05 06:20:15` – BizzyBob Jan 13 '17 at 03:13

1 Answers1

0

Okay, just for fun.

1. Parse the Data:

You could load the CSV data into an associative array like this (admittedly not very sophisticated, no error checking, validation etc):

$file = file_get_contents('CSV.csv');   // load csv into string
$rows = explode(PHP_EOL, $file);        // break each line into array
$data = Array();                        // where we'll store the data

foreach ($rows as $i => $row) {

    $fields = explode(',', $row);       // break each field into array

    foreach ($fields as $col => $val) {

        if ($i == 0) {
            $names[] = $val;            // headers in first row
        } else {
            $colName = $names[$col];

            if ($colName == 'Reg Date') $val = date('Y-m-d H:i:s', $val);

            $data[$i - 1][$colName] = $val;
        }
    }
}

So with your sample data above, you get:

array(4) {
  [0]=>
  array(4) {
    ["ID"]=>
    string(1) "1"
    ["Reg Date"]=>
    string(19) "2010-07-05 06:20:15"
    ["FirstName"]=>
    string(6) "Sergio"
    ["LastName"]=>
    string(7) "Roberto"
  }
  [1]=>
  array(4) {
    ["ID"]=>
    string(1) "2"
    ["Reg Date"]=>
    string(19) "2014-03-24 03:15:21"
    ["FirstName"]=>
    string(3) "Ray"
    ["LastName"]=>
    string(7) "Wilkins"
  }
  [2]=>
  array(4) {
    ["ID"]=>
    string(1) "3"
    ["Reg Date"]=>
    string(19) "2011-03-16 04:55:26"
    ["FirstName"]=>
    string(7) "Trueman"
    ["LastName"]=>
    string(3) "Ted"
  }
  [3]=>
  array(4) {
    ["ID"]=>
    string(1) "4"
    ["Reg Date"]=>
    string(19) "2013-07-17 11:58:12"
    ["FirstName"]=>
    string(4) "Volt"
    ["LastName"]=>
    string(4) "John"
  }
}

2. Sort the Data:

You can use usort() to sort by the timestamp column.

usort($data, function ($a, $b) {
    if ($a['Reg Date'] == $b['Reg Date']) return 0;
    return ($a['Reg Date'] < $b['Reg Date']) ? -1 : 1;
});

Results:

===After Sorting:===
array(4) {
  [0]=>
  array(4) {
    ["ID"]=>
    string(1) "1"
    ["Reg Date"]=>
    string(19) "2010-07-05 06:20:15"
    ["FirstName"]=>
    string(6) "Sergio"
    ["LastName"]=>
    string(7) "Roberto"
  }
  [1]=>
  array(4) {
    ["ID"]=>
    string(1) "3"
    ["Reg Date"]=>
    string(19) "2011-03-16 04:55:26"
    ["FirstName"]=>
    string(7) "Trueman"
    ["LastName"]=>
    string(3) "Ted"
  }
  [2]=>
  array(4) {
    ["ID"]=>
    string(1) "4"
    ["Reg Date"]=>
    string(19) "2013-07-17 11:58:12"
    ["FirstName"]=>
    string(4) "Volt"
    ["LastName"]=>
    string(4) "John"
  }
  [3]=>
  array(4) {
    ["ID"]=>
    string(1) "2"
    ["Reg Date"]=>
    string(19) "2014-03-24 03:15:21"
    ["FirstName"]=>
    string(3) "Ray"
    ["LastName"]=>
    string(7) "Wilkins"
  }
}

3. Filter the Data:

And you can use array_filter to filter the results down. I created a class to handle this (as described by @jensgram answer here)

$from = '2011-01-01';
$to = '2013-12-31';

$filtered = array_filter($data, Array(new compareDates($from, $to), 'isInRange'));

Results:

===After Filtering [2011-01-01 - 2013-12-31]:===
array(2) {
  [1]=>
  array(4) {
    ["ID"]=>
    string(1) "3"
    ["Reg Date"]=>
    string(19) "2011-03-16 04:55:26"
    ["FirstName"]=>
    string(7) "Trueman"
    ["LastName"]=>
    string(3) "Ted"
  }
  [2]=>
  array(4) {
    ["ID"]=>
    string(1) "4"
    ["Reg Date"]=>
    string(19) "2013-07-17 11:58:12"
    ["FirstName"]=>
    string(4) "Volt"
    ["LastName"]=>
    string(4) "John"
  }
}

Here's the simple class I used:

class compareDates
{
    function __construct($from, $to)
    {
        $this->from = $from;
        $this->to = $to;
    }

    function isInRange($ele)
    {
        if ($ele['Reg Date'] >= $this->from && $ele['Reg Date'] <= $this->to) {
            return TRUE;
        }

        return FALSE;
    }
}

NOTE: for the purpose of being able to see the date/time, I stored as a string. But you would normally just use the numeric value to sort by. I mention this to say that if you change the format of the date string, it could break the sorting.

Community
  • 1
  • 1
BizzyBob
  • 12,309
  • 4
  • 27
  • 51