php list array by random date

时间:2011-02-16 14:08:31

标签: php arrays list date random

我需要按日期列出一个数组,这就是我到目前为止所发布的图像名称并给出一个随机日期:

$images = glob('images/{*.jpg,*.gif,*.png}', GLOB_BRACE);

foreach($images as $image) 
 {
  $image  = str_replace('images/','',$image);
  $date   = date('Y-m-d', strtotime( '-'.mt_rand(0,1351).' days'));
  $result = $image . '<br />' . $date . '<br /><br />';

echo $result;

}

示例:

    16631824.jpg
    2008-04-23

    17122028.jpg
    2007-12-31

    1854815.jpg
    2007-10-13

    1gaffuv.jpg
    2009-04-12

    3rekel7c.jpg
    2010-06-13

现在我只需要帮助按日期列出它......一如既往地感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

$images = glob('images/{*.jpg,*.gif,*.png}', GLOB_BRACE);

$sorted = array();
foreach($images as $image) 
{
  $image  = str_replace('images/','',$image);
  $timestamp   = strtotime('-'.mt_rand(0,1351).' days');
  $sorted[$timestamp] = $image;
}

ksort($sorted);

foreach ( $sorted as $timestamp => $image )
{
  $date = date('Y-m-d', $timestamp);
  $result = $image . '<br />' . $date . '<br /><br />';
  echo $result;
}

答案 1 :(得分:2)

使用日期作为键创建图像数组,然后在输出之前对键进行排序。

$images_arr = array();
foreach($images as $image)
{
  $date   = date('Y-m-d', strtotime( '-'.mt_rand(0,1351).' days'));
  $images_arr[$date] = str_replace('images/','',$image);
}
// Sort on array keys
ksort($images_arr);

// Output your array however you need to
foreach($images_arr as $img)
{
  // output
}