how to display data based on this month only automatically?

  filtering, mysql, php

date range and table

I have a lot of data in a table, if I want to see it without any filter then the table will process long and heavy. so I want when I open a page that contains this table it only opens the data for this month.

<div class="col-md-5">
<div class="input-group">
    <input type="date" class="form-control" name='fromDate' placeholder='From Date' autocomplete='off' value='<?php if(isset($_POST['fromDate'])) echo $_POST['fromDate']; ?>' required>
    <span class="input-group-text">S/D</span>
    <input type='date' class='form-control' name='endDate' placeholder='To Date' autocomplete='off' value='<?php if(isset($_POST['endDate'])) echo $_POST['endDate']; ?>' required>
</div>
</div>

above is the form to set the date range when I want to filter the data.

<tbody>
        <?php
        include_once('system/koneksi.php');
        $sql = "SELECT * FROM harianproses WHERE 1 ";
                            
        // Date filter
        if(isset($_POST['filter'])){
        $from       = $_POST['fromDate'];
        $fromDate   = date("d/m/Y", strtotime($from));
        $end        = $_POST['endDate'];
        $endDate    = date("d/m/Y", strtotime($end));
        $dept       = isset($_POST['departemen']) ? $_POST['departemen'] : [];
        $unit       = isset($_POST['unit']) ? $_POST['unit'] : [];
        $subunit        = isset($_POST['subunit']) ? $_POST['subunit'] : [];
                                        
        if(!empty($fromDate) && !empty($endDate)){
          $sql .= "AND date_hp between '".$fromDate."' and '".$endDate."' ";
        }
        if(!empty($dept)){
          $sql .= "AND dept_hp = '".$dept."'";
        }
        if(!empty($unit)){
          $sql .= "AND unit_hp = '".$unit."'";
        }
        if(!empty($subunit)){
            $sql .= "AND subunit_hp = '".$subunit."'";
        }
}
                            
        $no = 1;
    
        $query = $connect->query($sql);
        while($row = $query->fetch_assoc()){
                                echo 
                                "<tr>
                                    <td>". $no++ ."</td>
                                    <td>".$row['date_hp']."</td>
                                    <td>".$row['dept_hp']."</td>
                                    <td>".$row['unit_hp']."</td>
                                    <td>".$row['subunit_hp']."</td>
                                    <td>".$row['shift_hp']."</td>
                                    <td>".$row['po_hp']."</td>
                                    <td>".$row['pono_hp']."</td>
                                    <td>".$row['ket_mat_hp']."</td>
                                    <td>".$row['type_mat_hp']."</td>
                                    <td>".$row['tout_hp']."</td>
                                    <td>".$row['lout_hp']."</td>
                                    <td>".$row['pout_hp']."</td>
                                    <td>".$row['pcsout_hp']."</td>
                                    <td>".$row['mlout_hp']."</td>
                                    <td>".$row['m2out_hp']."</td>
                                    <td>".$row['m3out_hp']."</td>
                                    <td>".$row['color_hp']."</td>
                                    <td>".$row['note_hp']."</td>
                                    <td>".$row['nopallet_hp']."</td>
                                    <td>".$row['quality_hp']."</td>
                                    <td>".$row['op_hp']."</td>
                                    <td>".$row['ht_hp']."</td>
                                    <td>".$row['hl_hp']."</td>
                                    <td>".$row['borong_hp']."</td>
                                    <td>".$row['pemborong_hp']."</td>
                                    <td>".$row['jenis_mat_hp']."</td>
                                    <td>".$row['supplier_hp']."</td>
                                    <td>".$row['nokirim_hp']."</td>
                                    <td>".$row['year_hp']."</td>
                                    <td>".$row['item_hp']."</td>
                                    <td>".$row['namakru_hp']."</td>
                                    <td>".$row['noprocess_hp']."</td>
                                    <td>".$row['nampan_hp']."</td>
                                    <td>".$row['ot_hp']."</td>
                                    <td>".$row['memo_hp']."</td>
                                    <td>".$row['ketproses_hp']."</td>
                                    <td>".$row['groupkualitas_hp']."</td>
                                    <td>".$row['speedpcs_hp']."</td>
                                    <td>".$row['speedml_hp']."</td>
                                    <td>".$row['inout_hp']."</td>
                                </tr>";
                            }
                        ?>
                    </tbody>

how to have an auto filtered table show only data for the current month?

Source: Ask PHP

LEAVE A COMMENT