php filemtime 24小时格式

php filemtime 24小时格式,php,date,format,Php,Date,Format,我创建这段代码是为了获取文件最后一次被触摸的日期,然后以AM/PM格式向用户显示它 但它似乎不起作用。我知道我很接近;我做错了什么 $filename = 'test.html'; if (file_exists($filename)) { $date = date(filemtime($filename)); clearstatcache(); } echo "- last updated: " . date('F d Y h:i A', strtotime($date));

我创建这段代码是为了获取文件最后一次被触摸的日期,然后以AM/PM格式向用户显示它

但它似乎不起作用。我知道我很接近;我做错了什么

$filename = 'test.html';
if (file_exists($filename)) {
    $date = date(filemtime($filename));
    clearstatcache();
}
echo "- last updated: " . date('F d Y h:i A', strtotime($date));
输出:上次更新时间:1969年12月31日下午6:59

if (file_exists($filename)) {
    $date = filemtime($filename);
    clearstatcache();
}
echo "- last updated: " . date('F d Y h:i A', $date);
在代码中,这一行:

$date = date(filemtime($filename));
因为
filemtime
返回一个UNIX时间戳,然后将该时间戳作为第一个参数传递给
date()
。即使这样做有效,您也要使用
strotime()
将该日期转换回UNIX时间戳,然后再转换回日期字符串,这似乎有点低效

也考虑如果文件不存在会发生什么,在代码的别处是否会设置“代码> >日期/代码>?

试试:

if (file_exists($filename)) {
    $date = filemtime($filename);
    clearstatcache();
}
echo "- last updated: " . date('F d Y h:i A', $date);
$date = date(filemtime($filename));
在代码中,这一行:

$date = date(filemtime($filename));
因为
filemtime
返回一个UNIX时间戳,然后将该时间戳作为第一个参数传递给
date()
。即使这样做有效,您也要使用
strotime()
将该日期转换回UNIX时间戳,然后再转换回日期字符串,这似乎有点低效

也考虑如果文件不存在会发生什么,在代码的别处是否会设置“代码> $DATE < /代码>?< /P>

$date = date(filemtime($filename));
那条线错了。
date()
的第一个参数是格式字符串。替换为:

$date = filemtime($filename);
此外,您不需要对时间戳执行
strotime()
,只需按原样使用即可:

echo date('F d Y h:i A', $date);
那条线错了。
date()
的第一个参数是格式字符串。替换为:

$date = filemtime($filename);
此外,您不需要对时间戳执行
strotime()
,只需按原样使用即可:

echo date('F d Y h:i A', $date);

该文件将始终存在,所以我不关心。我正在试你的办法。请稍等。在这种情况下,也许可以将回声移到if语句中?代码工作起来很有魅力。感谢简单的修复:)我会尽快接受(在接下来的4分钟内被阻止)该文件将始终存在,所以我不担心。我正在试你的办法。请稍等。在这种情况下,也许可以将回声移到if语句中?代码工作起来很有魅力。感谢您的简单修复:)我会尽快接受(在接下来的4分钟内被阻止)