0

I have a csv file with the following columns:

date, time, info1, info 2, info 3, info 4, info 5,

The file can be 1000 lines long or just have 3 lines. Every 15 seconds a new line is made with the above columns. But only when the devide is on.

A user should trim the csv file to the the specific start and end date/time he has choosen from a calendar or dropdownboxes. so let's say from 3 march 2016 10:00 till 7 may 2016 17:30.

is this possible with a csv file? I know you can read and write to a csv with fgetcsv, fopen and fwrite. But how to get the specific lines within the date range.. I have no clue.

Can someone point me in the right direction? Any help or advice is appreciated :-)

  • did you try something? post some code please so we can debug it and/or help you with it – rsz May 23 '16 at 13:09

1 Answers1

0
<?php
    $file = '/path/to/file/csv';
    $separator = ';';
    $length = 2048;
    $newFile = [];

    $i  =   0;
    if(($handle = fopen($file, 'r')) !== false){
        while(($data = fgetcsv($handle, $length, $separator)) !== false) {
            if($i == 0){
                $newFile[]  =   implode($separator, $data);
                $i++;
            }else{
                $fileDate   =   $data[0].' '.$data[1];

                if(check_in_range('03-03-2016 10:00:00', '07-05-2016 17:30', $fileDate)){
                    $newFile[]  =   implode($separator, $data);
                }
            }
        }
    }

    $newCsvFileData =   implode(PHP_EOL, $newFile);
?>

Function check_in_rage() comes from How to check if a date is in a given range? posted by @ConroyP

Community
  • 1
  • 1
Maytyn
  • 122
  • 1
  • 10