Home > Mobile >  How to count the quantity of rows in CSV where in column find the today's date?
How to count the quantity of rows in CSV where in column find the today's date?

Time:09-10

I have a file CSV: https://metalval.ru/account/data/stat/all.csv. Needs to print the sum (count) of rows where is current date ($today) and count of rows where current week to show in stat's dashboard the quantity of orders today and on this week.

Some example is a test code in work:

<?php
if (($handle = fopen("https://metalval.ru/account/data/stat/all.csv", "r")) !== FALSE) {
$today = date("d.m.Y");
$row = 0;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
 if ($data[2] == $today) {
   $row  ;
 }
 else {
   $row = 0;
 }
}
fclose($handle);
echo $row;
}
?>

CodePudding user response:

I just had to remove the else condition, because every time that $data[2] was different than $today the counter was reseted.

if (($handle = fopen("https://metalval.ru/account/data/stat/all.csv", "r")) !== FALSE) {
    $today = date("d.m.Y");
    $row = 0;
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if ($data[2] == $today) {
            $row  ;
        }
    }
    fclose($handle);
    echo $row;
}

CodePudding user response:

I try to count quantity of orders by checking date on this week using Monday and Next Monday as period means this week, but if-condition not works correctly:

    <?php
if (($handle = fopen("https://metalval.ru/account/data/stat/all.csv", "r")) !== FALSE) {
    $monday = date("d.m.Y", strtotime('last Monday'));
    $nextMonday = date("d.m.Y", strtotime('next Monday'));
    $today = date("d.m.Y");
    $row = 0;
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if (($monday = $data[2]) or ($monday < $data[2]) or ($today == $data[2]) or ($today < $nextMonday)) {
            $row  ;
        }
    }
    fclose($handle);
    echo '<li >За неделю всего ';
    echo $row;
    echo ' заявок.</li>';
}
?>
  • Related