Как отсортировать изображения по дате изменения?

Этот код сортирует изображения по алфавиту. Как сделать сортировку по дате? Так что пользователям будет удобнее просматривать новое изображение.

Вот код:

<link href="assets/css/gallery.css" rel="stylesheet">
<link href="assets/css/dope.css" rel="stylesheet">
<div class="row center">
<ul class="uk-tab" id="gamemodelistregion" uk-switcher="connect: + ul; animation: uk-animation-fade">
<li aria-expanded="true" class="uk-active">
<a>All skins</a>
<?php
# Skin directory relative to include/gallery.php (this file)
$skindir = "../skins/";

# Skin directory relative to index.html
$skindirhtml = "./skins/";

$images = scandir($skindir);

foreach($images as $curimg) {
if (strtolower(pathinfo($curimg, PATHINFO_EXTENSION)) == "png") {
?>
<li class="skin" onclick="$('#nick').val('{' + $(this).find('.title').text() + '} ');" data-dismiss="modal">
<div class="circular" style='background-image: url("./<?php echo $skindirhtml.$curimg ?>")'></div>
<h4 class="title"><?php echo pathinfo($curimg, PATHINFO_FILENAME); ?></h4>
</li>
<?php
}
}
?>
</li>
</ul>
</div>

1

Решение

filemtime функция возвращает метку времени Unix даты последней модификации файла.

// retrieve all images but don't waste time putting them in alphabetical order
$images = scandir($skindir, SCANDIR_SORT_NONE);

// remove all non-existent files and non-PNG files
$images = array_filter($images,
function($file) use ($skindir) {
$path = "$skindir/$file";
return file_exists($path) && strtolower(pathinfo($path, PATHINFO_EXTENSION)) == "png";
}
);

// sort image by modification time in descending order
usort($images, function($a,$b){return filemtime($a)-filemtime($b);});

// now you can iterate through images and print them
foreach($images as $curimg): ?>
<!-- Output image HTML here -->
<?php
endforeach;
1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector