How to check which date is the latest?

$files = scandir(__DIR__, SCANDIR_SORT_DESCENDING);

At first I tried to check if there is a sort type for the date, but unfortunately I could not find it, so I decided to use filemtime

$latest = date("d Y H:i:s.");
printf($latest);
foreach($files as $value) {
    if ($value != "..") {
        if($value != ".") {
            $latestnew = date("d Y", filemtime($value));

            if($latestnew > $latest) {
                $latest = $value;
            }
        }
    }
}
printf($latest);

You can see that I have an array in which there are a lot of files. The last file name should be in the $ last variable. I know that checking ">" does not work, but I could not find another solution. Thank.

+4
source share
2 answers

, , , - , , . , , . .

$latest = 0;
$latest_name = null;

foreach($files as $value) {
    if (($value != "..") && ($value != ".")) {
        $latestnew = filemtime($value);

        if($latestnew > $latest) {
            $latest = $latestnew;
            $latest_name = $value;
        }
    }
}

print(date("d Y H:i:s", $latest) . ' - ' . $latest_name);

. $value , filemtime. filemtime($path . '/' . $value);

+1

PHP:

int filemtime ( $filename)

FALSE . Unix, () .

, .

$latestFilename = '';
$latestTime = 0;    
foreach($files as $filename) {
        if ($filename != "..") {
            if($filename != ".") {
                $currentFileTime = filemtime($filename);

                if($currentFileTime > $latestTime) {
                    $latestFilename = $filename;
                    $lastestTime = $currentFileTime;
                }
            }
        }
    }

- DateTime .

+2

Source: https://habr.com/ru/post/1688603/


All Articles