[英]How do I get how long the file was created in seconds in PHP
我正在嘗試創建一個腳本來刪除 1 天前創建的文件,我需要計算該文件在幾秒鍾內創建了多長時間,但我不知道如何,有人可以幫忙嗎?
來自: https://www.php.net/manual/en/function.filectime.php
您可以使用filectime
function 來獲取文件的創建日期。 function 返回 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 那樣存在。
以下腳本顯示文件夾 /path 上 +1 天內更改的文件:
<?php
$path_to_dir ='/path/';
$timenow = time();
if ($handle = opendir($path_to_dir)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !is_dir("$path_to_dir$entry")) {
$modified_date = filemtime("$path_to_dir$entry");
if (($timenow - $modified_date)> 86400){
echo "$path_to_dir$entry\n";
}
}
}
closedir($handle);
}
?>
使用filectime
。 對於 Windows,它將返回創建時間,對於 Unix,更改時間是您可以獲得的最佳時間,因為在 Unix 上沒有創建時間,祝你好運:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.