获取在特定日期或今天使用php修改过的所有文件

时间:2015-03-18 10:20:02

标签: php

我的问题是找到在指定日期或今天修改的文件。

这里我写了以下代码来获取文件并将它们排序到文件修改日期

$files = glob(DIRPATH.'/*.*');
usort($files, function($a, $b) {
        return filemtime($a) < filemtime($b);
    });

我的输入文件是

raj1-18032015.csv was last modified: March 18 2015 02:31:04. 
ani-18032015.csv was last modified: March 18 2015 05:46:47. 
mahesh-18032015.csv was last modified: March 18 2015 00:54:10. 
suraj-18032015.csv was last modified: March 18 2015 23:49:04. 
raj1-15032015.csv was last modified: March 15 2015 23:38:46. 
vaibhav-13032015.csv was last modified: March 13 2015 
kamal-12032015.csv was last modified: March 12 2015 02:48:22. 
siraj-11032015.csv was last modified: March 11 2015 23:56:54. 

和输出文件需要是这些

raj1-18032015.csv was last modified: March 18 2015 02:31:04. 
ani-18032015.csv was last modified: March 18 2015 05:46:47. 
mahesh-18032015.csv was last modified: March 18 2015 00:54:10. 
suraj-18032015.csv was last modified: March 18 2015 23:49:04. 

请帮助

1 个答案:

答案 0 :(得分:3)

你可以试试这个:

/**
  * @param Array  : array of files path and name
  * @param String : date selector 
  * @param String : optional, the passed date format default is m:d:Y ex, 09:30:2015 @link http://php.net/manual/en/function.date.php for more options
  * @return Array : array of filtered files path and name
  */
function fileFilter ($files, $date, $format = 'm:d:Y') {
    $selectedFiles = array ();

    foreach ($files as $file) {
        if (date ($format, filemtime ($file)) == $date) {
            $selectedFiles[] = $file; 
        }
    }
    return $selectedFiles;
}
// example :
var_dump(fileFilter (glob("C:/*.*"), "09:28:2013"));