[thelist] PHP File time/date stamps

gabriel rivera evolt at protocol0.com
Tue Feb 18 22:24:17 CST 2003


Instantiate a directory object and loop through it with the dir->read()
call.  As you loop through, populate a new multidemensional array or object
with the filenames, whatever other data, and the time.

$d = dir($the_dir_to_loop_thru);

$c = 0; // array counter

while($current_file = $d->read()) {

    //  don't want the dot files

    if ($current_file != "." && $current_file != "..") {

        $files[$c]['name'] = $current_file ;

        // get the last mod time, we need it as a unix timestamp so we
        // can sort it

        $files[$c]['lastmod_time'] = strtotime(filemtime($current_file));

        $c ++ ;

    }

}

// now we need a sort function

function time_cmp ($a, $b) {

    return strcmp($a['lastmod_time'], $b['lastmod_time']);

}

// sort the array

usort ($files, "time_cmp");

...and so forth

http://www.php.net/manual/en/class.dir.php
http://www.php.net/manual/en/function.filemtime.php
http://www.php.net/manual/en/function.strtotime.php

> I need some sort of PHP function that will look through a directory
> of files for me and find the most recent. I guess its a kinda sort on
> the timestamp...
>
> I have no idea how to access timestamps or to sort them in PHP so
> any help would be appreciated?
>
> It's for a bespoke simple CMS system where no DB is available
>
> Cheers
> -Dan
>




More information about the thelist mailing list