首页 > 解决方案 > 如何获取在 PHP 中以秒为单位创建文件的时间

问题描述

我正在尝试创建一个脚本来删除 1 天前创建的文件,我需要计算该文件在几秒钟内创建了多长时间,但我不知道如何,有人可以帮忙吗?

标签: php

解决方案


来自:https ://www.php.net/manual/en/function.filectime.php

您可以使用该filectime功能获取文件的创建日期。该函数返回一个 unix 时间戳。然后,您可以从当前时间中减去该时间,以获得文件创建前的秒数。

<?php

# Get the creation date of the file in unix timestamp
$creationDate = filectime("myfile.txt");

# Subtract the creation date from the current time, to get the time difference in seconds
$secondsAgo = time() - $creationDate;

?>

请注意,在大多数 unix 系统上,它将返回最后修改日期而不是创建日期。这是因为在大多数 unix 系统上,创建日期并不像在 Windows 上那样存在。


推荐阅读