Date/time format not working - can't use in filename

Viewed 31

I've tried searching for the solution but nothing quite fit the issue.

I have a function which exports data from my SQL Database and writes it into a CSV file. The name of the file includes the current date.

When I use date format Y-m-d then it works but I need the time as well, when I add time it doesn't work and I get an error.

Here is the code

public function exportCSV()
{
    try {
        $this->db_table = $this->get_data_from_query('SELECT * FROM klassenraeume  ORDER BY id asc');

        if($this->db_table->num_rows > 0)
        {
            $folder = "exportiert/";
            $exported_file = $folder."csv_".date("Y-m-d").".csv";
            $file_creater = fopen($exported_file, 'w');

            foreach ($this->db_table as $data)
            {
                fputcsv($file_creater, $data);
            }

            fseek($file_creater, '0');
            echo "<p style='-webkit-text-fill-color: #26f308; font-size: 2em; '>Daten erfolgreich exportiert!</p>";
        }
    }
    catch (Exception $e)
    {
        echo "Error";
    }
}

I've tried this:

$exported_file = $folder."csv_".date("Y-m-d H:i:s").".csv";

and I've tried this way:

public function exportCSV()
{
    try {
        $this->db_table = $this->get_data_from_query('SELECT * FROM klassenraeume  ORDER BY id asc');

        if($this->db_table->num_rows > 0)
        {
            $date = date("Y-m-d H:i:s");
            $folder = "exportiert/";
            $exported_file = $folder."csv_". $date .".csv";
            $file_creater = fopen($exported_file, 'w');

            foreach ($this->db_table as $data)
            {
                fputcsv($file_creater, $data);
            }

            fseek($file_creater, '0');
            echo "<p style='-webkit-text-fill-color: #26f308; font-size: 2em; '>Daten erfolgreich exportiert!</p>";
        }
    }
    catch (Exception $e)
    {
        echo "Error";
    }
}

Nothing has worked so far. I've also tried putting $date into quotations etc.

The error message is this:

Warning: fopen(exportiert/csv_2022-06-23 12:51:28.csv): Failed to open stream: No such file or directory in C:\xampp\htdocs\CSV_Upload_Test\projekt\klassen_funktionen\Daten.inc.php on line 141

Fatal error: Uncaught TypeError: fputcsv(): Argument #1 ($stream) must be of type resource, bool given in C:\xampp\htdocs\CSV_Upload_Test\projekt\klassen_funktionen\Daten.inc.php:144 Stack trace: #0 C:\xampp\htdocs\CSV_Upload_Test\projekt\klassen_funktionen\Daten.inc.php(144): fputcsv(false, Array) #1 C:\xampp\htdocs\CSV_Upload_Test\projekt\Bestehende_DB_Eintraege_anzeigen.php(75): Daten->exportCSV() #2 {main} thrown in C:\xampp\htdocs\CSV_Upload_Test\projekt\klassen_funktionen\Daten.inc.php on line 144

What am I doing wrong?

1 Answers

Since you're using Windows, colons (:) are not allowed in a filename. This is a feature of the Windows filesystem.

So you can simply use some other character instead. In a date / time string, it's a commonly accepted convention to use a dot (.) as a separator between the the time components, so that is an approach you could take and which would likely to be understood by most people.

So simply:

$date = date("Y-m-d H.i.s");

See also Make filename with colon (“:”) in Windows and others.

Related