【问题标题】:PHP: how to subtract e.g 9 days from filectime()PHP:如何从 filectime() 中减去例如 9 天
【发布时间】:2015-02-22 23:06:33
【问题描述】:

我想删除所有超过 9 天的文件。由于 filectime 返回一个

的创建时间

某个文件,因此,我只想知道如何减去一个例子:9天

filectime().

代码

<?php
 $filename = 'JSONFormate.json';
 if (file_exists($filename)) {
echo "$filename was last changed: " . date("F D Y H:i:s.", filectime
($filename));
echo "\n......".filectime($filename)-777600;
}
?>

结果 JSONFormate.json 最后更改时间:2012 年 5 月星期二 22:50:10.-777600

【问题讨论】:

标签: php


【解决方案1】:

filectime() 的结果中减去 777600 (9 days in seconds)

【讨论】:

  • 答案中的链接向您展示了如何计算秒数。或者你可以只做 60*60*24*9 = 777600
  • 请检查我现在发布的修改。我收到发布的结果
  • 试试把filectime($filename)-777600放在括号里?
【解决方案2】:

filectime() 返回文件的最后修改时间,而不是创建时间。

此外,您不想让filectime() 休息 9 天。您要做的是从现在开始删除 9 天,看看它是否大于返回的 filectime()

int $last_modified = filectime("...");
int $nine_days_ago = time() - 777600;
if($nine_days_ago < $last_modified) {
  ...
}

【讨论】:

    【解决方案3】:

    我会推荐使用像 find . -name "*.trc" -mtime +9 -exec rm -rf {} \; 这样的 unix 命令 您可以使用“`”(反引号)或 exec 或 system 在 PHP 中创建一个 cronjob 甚至运行它

    【讨论】:

      【解决方案4】:

      filectime 返回一个 unixtimestamp。如果计算 9 天的 unixtimestamp。然后您可以从您从 filectime 收到的 unixtimestap 中减去 9 天的 unixtimestamp。那么你会得到一个超过 9 天的 unixtimestamp。

      然后您检查所有时间戳低于计算时间戳的文件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-03
        • 2011-11-30
        • 2010-11-20
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多