0

I am new to php i have created a dashboard where the client has to download the data by selecting the date. And the date format which is stored in our database is "d-m-Y H:i:s" and when exporting the format is like "Y-M-D" and when changed the code to "d-m-Y" the data shows blank. So any one help me how to do it. Below is my code for exporting the data in csv format.

    <?php
        include('connection.php');    
        //export csv file    
        $from= $_POST['from'].' 00:00:00';
        $to= $_POST['to'].' 23:59:59';
        $today = date("d-m-Y");
        $end = date("d-m-Y");
        $start = (isset($_POST['from'])) ? $_POST['from'] : '$today';    
        $end = (isset($_POST['to'])) ? $_POST['to'] : '$end';
        //echo $start;
        //echo $end;
        if(isset($_POST["export"])) {    
            header("Content-type: text/csv; charset=utf-8");
            header("Content-disposition: attachment; filename=data-$start-$end.csv");
            $output = fopen("php://output", "w");
            fputcsv($output, array('SL NO','Mobile','Prescription','Address','Landmark','City','Date and Time'));
            $query = "SELECT id,number,upload,address,landmark,city,entered_date FROM usedetails WHERE entered_date BETWEEN '" . $from. "' AND '" . $to. "'";
            $result = mysqli_query($conn,$query);
            while($row = mysqli_fetch_assoc($result)) {
               fputcsv($output, $row);
            }
            fclose($output);
        }    
    ?>
Ritesh
  • 3
  • 2
  • So are you saying "Y-M-D" works and "d-m-Y" does not? Why don't you just use the former then? This would also be the [recommended ISO format](https://en.wikipedia.org/wiki/ISO_8601), which (at least in theory) should have the best compatibility. – Karsten Koop Jul 24 '18 at 13:32
  • Possible duplicate of [Date format that is guaranteed to be recognized by Excel](https://stackoverflow.com/questions/2591925/date-format-that-is-guaranteed-to-be-recognized-by-excel) – Karsten Koop Jul 24 '18 at 13:34

0 Answers0