2017-08-28 186 views
1

此代码按字母顺序排列图像。如何按日期排序?因此,用户将更方便地查看新图像。如何根据修改的日期对图像进行排序?

下面是代码:

<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

的可能的复制[SCANDIR()按日期排序改性(https://stackoverflow.com/questions/11923235/scandir-to-sort-by-date-modified) –

回答

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; 
+0

不工作。没有错误,图像和标题不显示。 – TopoR

+0

@salathe你为什么做这个编辑?在OP代码中 - 实际上是显示图像,只是按错误的顺序 - pathinfo函数被提供给'$ images'中的每个成员。这就是我正在做的 – BeetleJuice

+0

@TopoR请立即尝试。 – BeetleJuice

相关问题