1

I read a csv file like this one :

  391;WETSUITS;07/07/2020 12:47:36;0021000001463;
  392;WETSUITS;07/07/2020 12:47:49;0021000007845;
             ....

I retrieve the one date with this format :

echo $data[2] = 07/07/2020 12:47:36

I would like to have two variables :

$date = 07-07-2020 or 2020-07-07
$time = 1247

I tried with str_replace for the first part but I get "Notice: Undefined offset: 2" as an error message, how can I change this?

 <?php

$csv = 'C:/wamp64/www/BI/BI1_20200707_1520_28044.csv';


if (($handle = fopen("$csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 9000000, ";")) !== FALSE){

            // echo $data[2]; //07/07/2020 12:47:36

        $new = str_replace('/', '_', $data[2]);
        echo $date = substr($new,0,20)."\n";;
 
    }
}

?>
  • 1
    Not reaaly clear for what line you get the notice. If `echo $data[2]` shows correct data, then `str_replace` should work too. – u_mulder Jul 07 '20 at 20:35
  • You asked this question earlier and it was closed as a duplicate of [this question](https://stackoverflow.com/questions/2167916). Your title would indicate this is a still a dupe but the error you mention is completely different. Which problem are you actually trying to solve? – John Conde Jul 07 '20 at 20:40
  • I have a error at the line "$new = str_replace('/', '_', $data[2]);" @u_mulder –  Jul 07 '20 at 20:50

1 Answers1

0

This notice may come because of an empty line at the end of file. So, just check if the $data array is empty.

while (($data = fgetcsv($handle, 9000000, ";")) !== false) {
        // empty line will be transformed to [null]
        if (empty(array_filter($data))) {
            continue;
        }

    // other operations
}

Advice: Better is to use DateTime for operations with dates. Date format reference: https://www.php.net/manual/en/function.date

$date = \DateTime::createFromFormat('d/m/Y H:i:s', $data[2]);
if ($date === false) {
    // handle date parse error
}

$date1 = $date->format('Y-m-d'); // 2020-07-07
$date2 = $date->format('Hi'); // 1247
Dmitry Solovov
  • 303
  • 1
  • 2
  • 9
  • This doesn't address the error message in their question – John Conde Jul 07 '20 at 20:42
  • First line does – Dmitry Solovov Jul 07 '20 at 20:44
  • I tried your code and I got a run-time error: "Fatal error: Uncaught Error: Call to a member function format() on boolean" @DmitrySolovov –  Jul 07 '20 at 20:46
  • You have to do something with date parsing errors (in an if-block with a comment `// handle date parse error`). Place `continue` or `break`, may be log the error. – Dmitry Solovov Jul 07 '20 at 20:50
  • If I put a break or a continue in the if instead of "// handle date parse error" I don't get anything displayed @Dmitry Solovov –  Jul 07 '20 at 21:03
  • How can I display the two variables $date1 and $date2? @DmitrySolovov –  Jul 07 '20 at 21:04
  • They are string variables, so `echo $date1; echo $date2;` – Dmitry Solovov Jul 07 '20 at 21:11
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/217416/discussion-between-eric27-and-dmitry-solovov). –  Jul 07 '20 at 21:13