0

I am building a cron job that scans a directory of images and resizes anything over 800 pixels wide. I also only want it to enter into the resize if the image was uploaded that day so I tried to use filemtime() but I keep getting 1969-12-31 back.

I have read every forum post I could find on the issue and nothing seems to work. I have tested it on my local WIN 10 machine (using xammplite) and on my production Linux machine. Both are using PHP 5.3 or better and I get the exact same result.

Here's a code snippet along with echos I have added to see what's going on:

$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
           }
       }
   }

The first echo in the foreach loop prints 1969-12-31 for all the images

The second echo in the foreach correctly prints the path

The third echo returns true for file exists but it fails to enter the if condition because of the date being wrong.

I have checked for warnings in the logs and there are none, I have checked folder permissions and they are all fine.

I just done get it! Maybe my eyes are missing something or there's something happening I am not privy to so I could really use some advice.

dc dalton
  • 63
  • 1
  • 11
  • What if you use this code instead: `date ("F d Y H:i:s.", filemtime($_SERVER['DOCUMENT_ROOT'] . "/forum_functions/image_uploads/{$file}")); ` – Hackerman Oct 21 '16 at 19:39

2 Answers2

0

You don't need to call strtotime() on the result of filemtime(). strtotime() is for taking a human-readable date/time string and turning it into a numeric timestamp, but filemtime() returns a timestamp, not a string.

   echo 'file date is ' . date('Y-m-d', filemtime($_SERVER['DOCUMENT_ROOT'] . "/forum_functions/image_uploads/{$file}")) . '<br>';
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

You might want to skip the 'strtotime'. According to PHP manual filemtime will return a value suitable for usage in date().

The 1969-12-31 is awfully close to 1-1-1970. The start of the unix timestamp. To close for comfort and coincidence. It seems like the strtotime filemtime combo produces a 0 which your server, due to daylight saving time or maybe time zones offsets and thus displays the December 31st 1969.

Pim Broens
  • 702
  • 1
  • 5
  • 16