我有這段代碼來顯示來自文件夾的圖像。所以任何人都知道如何添加函數來按日期將所有圖像分類到我的代碼中。 我已經測試過放入類似krsort($ file)的東西;或asort($文件); 但我不知道在哪裏把它放在工作。任何幫助都會很棒。謝謝。如何按日期排序文件
<?php
$directory = $words[$lang]["folder_link"];
$allowed_types=array('jpg','jpeg','gif','png','JPG');
$file_parts=array();
$ext='';
$title='';
$i=0;
$dir_handle = @opendir($directory) or die("There is an error with your image directory!");
while ($file = readdir($dir_handle))
{
if($file=='.' || $file == '..') continue;
$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));
$title = implode('.',$file_parts);
$title = htmlspecialchars($title);
$nomargin='';
if(in_array($ext,$allowed_types))
{
if(($i+1)%4==0) $nomargin='nomargin';
echo '
<div class="pic '.$nomargin.'" style="background:url('.$directory.'/'.$file.') no-repeat 50% 50%;">
<a href="'.$directory.'/'.$file.'" title="'.$title.'" target="_blank">'.$title.'</a>
</div>';
$i++;
}
}
closedir($dir_handle);
?>
你是什麼意思?文件修改日期? – tan
文件修改日期 –
[很多](https://stackoverflow.com/questions/2667065/sort-files-by-date-in-php)[of](https://stackoverflow.com/questions/2084986/file -creation-time)[Qs](https://stackoverflow.com/questions/18089172/sort-file-list-by-date)[cover](https://stackoverflow.com/questions/10021458/sorting-directory -files-by-creation-date-in-php)[this](https://stackoverflow.com/questions/33767060/how-to-sort-by-date-using-php-opendir)[topic](https) ://stackoverflow.com/questions/124958/glob-sort-by-date)[已經](https://stackoverflow.com/questions/7948300/order-this-array-by-date-modified)(from a快速搜索)。 – salathe