vendredi 2 juillet 2021

Why some of date format are not correct in excel sheet in php laravel?

I am exporting csv file one of my column is date but date format get change for some rows i am not getting it why it should be d-m-Y H:i:s but giving d/m/Y H:i:s

enter image description here

here is my code

    $headers = array(
      "Content-type" => "text/csv",
      "Content-Disposition" => "attachment; filename=Consignment Income Invoice.csv",
      "Pragma" => "no-cache",
      "Cache-Control" => "must-revalidate, post-check=0, pre-check=0",
      "Expires" => "0"
    );

    $callback = function () use ($consignments, $sheet_header_columns, $db_columns) {
      $output = fopen("php://output", "wb");
      fputcsv($output, $sheet_header_columns);

      foreach ($consignments as $consignment) {
        $sheet_row = [];
        foreach ($db_columns as $column) {
          if ($column == 'customers') {
            $column_value = $consignment->customers->name;
          } elseif ($column == 'delivery_runs') {
            $column_value =  $consignment->delivery_runs->name ?? '';
          } elseif ($column == 'pickup_address') {
            $column_value =  $consignment->pickup_addresses->full_address ?? '';
          } elseif ($column == 'delivery_address') {
            $column_value =  $consignment->delivery_addresses->full_address ?? '';
          } elseif ($column == 'income') {
            $column_value =  $consignment->charges->sum('income') ?? '';
          } elseif ($column == 'driver_name') {
            $column_value = $consignment->run_sheets->drivers->name ?? '';
          } elseif ($column == 'date_delivered') {
            $column_value =  setDateTimeFormat($consignment->date_delivered);
          } elseif ($column == 'created_at') {
            $column_value = setDateTimeFormat($consignment->created_at);
          } elseif ($column == 'delivery_date') {
            $column_value =  setDateFormat($consignment->delivery_date);
          } else {
            $column_value =   $consignment->$column;
          }
          array_push($sheet_row,$column_value);
        }
        fputcsv($output, $sheet_row); // here you can change delimiter/enclosure
      }

      fclose($output);

    };
    return response()->stream($callback, 200, $headers);

setDateTimeFormat() function

function setDateTimeFormat($date)
{
    if (!empty($date)) {
        $date = date(getDateTimeFormat(), strtotime($date));
        $date = str_replace('-','/',$date);
        if ($date == "01-01-1970 10:00:00") {
            return "";
        }
        return $date;
    } else {
        return "";
    }
}

I set date time formate before exporting it. but still it is changing the format don't know why.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire