【问题标题】:PHP filemtime() returning 1969-12-31 - cant find issuePHP filemtime() 返回 1969-12-31 - 找不到问题
【发布时间】:2016-10-21 19:35:27
【问题描述】:

我正在构建一个 cron 作业,它扫描图像目录并调整超过 800 像素宽的任何内容。如果那天上传了图像,我也只希望它进入调整大小,所以我尝试使用 filemtime() 但我一直得到 1969-12-31 回来。

我已经阅读了我能找到的关于该问题的所有论坛帖子,但似乎没有任何效果。我已经在我的本地 WIN 10 机器(使用 xammplite)和我的生产 Linux 机器上对其进行了测试。两者都使用 PHP 5.3 或更高版本,我得到完全相同的结果。

这是一个代码 sn-p 以及我添加的回声以查看发生了什么:

$files = scandir($_SERVER['DOCUMENT_ROOT'] . '/forum_functions/image_uploads');

$check_date = date('Y-m-d', strtotime('now'));

echo 'check date is ' . $check_date . '<br>';

foreach ($files as $file)   {

    if ($file != '.' && $file != '..')   {

        echo 'file date is ' . date('Y-m-d', strtotime(filemtime($_SERVER['DOCUMENT_ROOT'] . "/forum_functions/image_uploads/{$file}"))) . '<br>';

        echo 'file is ' . $_SERVER['DOCUMENT_ROOT'] . "/forum_functions/image_uploads/{$file}" . '<br>';

        echo 'file exists ' . file_exists($_SERVER['DOCUMENT_ROOT'] . "/forum_functions/image_uploads/{$file}") . '<br>';

        if (file_exists($_SERVER['DOCUMENT_ROOT'] . "/forum_functions/image_uploads/{$file}") && $check_date == date('Y-m-d', strtotime(filemtime($_SERVER['DOCUMENT_ROOT'] . "/forum_functions/image_uploads/{$file}"))))  {
          // do resizing
           }
       }
   }

foreach 循环中的第一个回显为所有图像打印 1969-12-31

foreach 中的第二个 echo 正确打印了路径

第三次回显文件存在返回true,但由于日期错误,未能进入if条件。

我检查了日志中的警告,但没有,我检查了文件夹权限,它们都很好。

我刚刚收到了!也许我的眼睛错过了什么,或者发生了一些我不知道的事情,所以我真的可以使用一些建议。

【问题讨论】:

  • 如果你改用这段代码会怎样:date ("F d Y H:i:s.", filemtime($_SERVER['DOCUMENT_ROOT'] . "/forum_functions/image_uploads/{$file}"));

标签: php filetime


【解决方案1】:

您无需根据filemtime() 的结果调用strtotime()strtotime() 用于获取人类可读的日期/时间字符串并将其转换为数字时间戳,但filemtime() 返回的是时间戳,而不是字符串。

   echo 'file date is ' . date('Y-m-d', filemtime($_SERVER['DOCUMENT_ROOT'] . "/forum_functions/image_uploads/{$file}")) . '<br>';

【讨论】:

    【解决方案2】:

    您可能想跳过“strtotime”。根据 PHP 手册 filemtime 将返回一个适合在 date() 中使用的值。

    1969-12-31 非常接近 1-1-1970。 unix 时间戳的开始。为了舒适和巧合而关闭。由于夏令时或时区偏移,strtotime filemtime 组合似乎会生成一个 0,因此您的服务器会显示 1969 年 12 月 31 日。

    【讨论】:

      猜你喜欢
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多