gpt4 book ai didi

php - 用PHP将日期和时间写入LOG文件

转载 作者:行者123 更新时间:2023-12-04 11:51:06 27 4
gpt4 key购买 nike

我想将当前日期和时间写入我的日志文件:

$logFileName = 'file://c:\MYLOG.log'; // /var/logs/file.log
$logContent = "Running through the function".PHP_EOL;
$date = (new DateTime('NOW'))->format("y:m:d h:i:s");
if ($handle = fopen($logFileName, 'a'))
{
fwrite($handle, $date);
fwrite($handle, PHP_EOL);
fwrite($handle, $logContent);
fwrite($handle, PHP_EOL);
fwrite($handle, $cmdWindows);
fwrite($handle, PHP_EOL);
fwrite($handle, $params);
fwrite($handle, PHP_EOL);
}
fclose($handle);

当它通过我的方法运行时,我可以看到所有我想要的信息,但不能看到日期和时间。
你能告诉我我错在哪里吗?先感谢您。

最佳答案

在您的数据时间构造函数中现在不是必需的,因为它是默认的。至于你的答案,你不能在构造函数上应用链接(在 PHP 5.4 之前)
如果你有 PHP > 5.4 你可以像这样应用构造函数链 (new Foo)->bar()
下面的示例肯定会起作用:

$date = new DateTime();
$date = $date->format("y:m:d h:i:s");

关于php - 用PHP将日期和时间写入LOG文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28297392/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com