Это должно сделать это:
<?php
foreach (glob('*.jpg') as $f) {
# store the image name with the last modification time and imagename as a key
$list[filemtime($f) . '-' . $f] = $f;
}
$keys = array_keys($list);
sort($keys); # sort is oldest to newest,
echo $list[array_pop($keys)]; # Newest
echo $list[array_pop($keys)]; # 2nd newest
Если вы можете сделать имена файлов YYYYMMDDHHMM.jpg, sort () может расположить их в правильном порядке, и это будет работать:
<?php
foreach (glob('*.jpg') as $f) {
# store the image name
$list[] = $f;
}
sort($list); # sort is oldest to newest,
echo array_pop($list); # Newest
echo array_pop($list); # 2nd newest