1

Does anyone know a way to grab the last modified dates of all files within a folder and compare it to a certain date?

So far I have this.

<?php
    $lastmoddate = (date("Ymd", filemtime($file)));
    $todaysdate = date("Ymd", time());

    $result = array(); 
    $folder = ('uploaded_files/');     
    $handle = opendir($folder);

    foreach (glob("$folder/*") as $team){$sort[]= end(explode('/',$team));}

    while (false !==($file = readdir($handle)))
    {
        if ( $file != ".." && $file != "." )
        {
            $file = "uploaded_files/".$file ;
            if (!is_dir($file))
                $result[] = $file;
        } 
    }
    closedir($handle);


    foreach ($result as $file){
        if ($lastmoddate > $todaysdate){
            if (strpos($file, "+12:00") !==false){
                echo "$file".",".date ("h:i d/m/Y", filemtime($file))."\r\n"."<br/>";
            }
        }
    }
?>

This doesn't work as $lastmoddate = gives me the date 1969 12 31.

Sean Walsh
  • 8,266
  • 3
  • 30
  • 38

2 Answers2

0

PHP's filemtime() (which internally basically just calls stat() and returns only the m-time value) works on a single file at a time.

You've already got a glob() call in your script to get a list of filenames. Put the filemtime() call inside that loop to get each file's mtime, and do the comparisons in there.

Your code is not working as you've not assigned a value to $file at the point you do the initial filemtime() call, so that returns a boolean FALSE for failure, which gets converted to an integer 0 for the date() formatting. You're in a timezone that's negative-GMT, so that converts to a date slightly BEFORE Jan 1/1970, which is time 0 in UTC.

What you need is:

foreach (glob("$folder/*") as $team) { 
    $lastmoddate = filemtime("$folder/$team");
    ... date stuff ...
    $sort[]= basename($team);
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

So far I can see 2 inconsistent things in your code.

  1. you are getting lastmoddate only once, not for the existing files but for some undefined (yet) $file

  2. you date copmparison makes no sense. Say, even if your file has been modified today, it's date never be more than today's date, so, all your comparisons will fail for sure. At least use >= or == to compare, not >

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • The files have been modified in a -10GMT and the website is based in a +10GMT. So $todaysdate means -10GMT. from what you can see... what would the $file value be? – user1026866 Nov 06 '11 at 06:33