日期/时间格式不起作用 - 可以在文件名中使用
我已经尝试搜索解决方案,但没有任何问题。
我的功能可以从我的SQL数据库中导出数据,然后将其写入CSV文件。 该文件的名称包括当前日期。
当我使用日期格式ymd
时,它可以正常工作,但是我也需要时间,当我添加时间时,它不起作用,并且会遇到错误。
代码
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";
}
}
这是我尝试过的
$exported_file = $folder."csv_".date("Y-m-d H:i:s").".csv";
:我已经尝试过这样的方法:
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";
}
}
到目前为止,什么都没有。我还尝试将$ date
放入报价等。
错误消息是:
警告:fopen(exportiert/csv_2022-06-23 12:51:28.csv):无法打开 流:没有此类文件或目录 C:\ Xampp \ htdocs \ csv_upload_test \ projekt \ klassen_funktionen \ daten.inc.inc.php 在第141行
上致命错误:uck offult typeError:fputcsv():参数#1($ stream)必须 是类型的资源,在 C:\ Xampp \ htdocs \ csv_upload_test \ projekt \ klassen_funktionen \ daten.inc.inc.php:144 堆栈跟踪:#0 C:\ Xampp \ htdocs \ csv_upload_test \ projekt \ klassen_funktionen \ daten.inc.php(144): fputcsv(false,数组)#1 C:\ Xampp \ htdocs \ csv_upload_test \ projekt \ bestehende_db_eintraege_anzeigen.php(75): daten-&gt; exportcsv()#2 {main}扔进 C:\ Xampp \ htdocs \ csv_upload_test \ projekt \ klassen_funktionen \ daten.inc.inc.php 在第144行
上
我在做什么错?
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 141Fatal 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于您使用的是Windows,因此在文件名中不允许结肠()。这是Windows文件系统的功能。
因此,您可以简单地使用其他字符。在日期 /时间字符串中,使用DOT(
。< / code>)作为时间组件之间的分隔符是一种普遍接受的惯例,因此您可以采用一种方法,并且可能会被理解大多数人。
因此简单地:
另请参阅在Windows中使用colon(“:”)制作文件名还有其他。
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:
See also Make filename with colon (“:”) in Windows and others.