使用php将所选日期与文件名进行比较

使用php将所选日期与文件名进行比较,php,Php,文件夹中有几千个文件。我想让用户选择一个日期,并根据选择的日期显示所有文件 文件格式为bt_16-08-30_00_22_03.db 这是我获取所有文件的方法 foreach (new DirectoryIterator($dbfilepath) as $file) { if ($file->isFile()) { $thefilename = $file->getFilename(); if (strripos($thefilename,".db")==t

文件夹中有几千个文件。我想让用户选择一个日期,并根据选择的日期显示所有文件

文件格式为
bt_16-08-30_00_22_03.db

这是我获取所有文件的方法

foreach (new DirectoryIterator($dbfilepath) as $file) {
   if ($file->isFile()) {
     $thefilename = $file->getFilename();
     if (strripos($thefilename,".db")==true) {
       $thefiles[$countdirfiles] = $thefilename;
       $countdirfiles++;
     }
   }
 }
我如何才能找到只有特定日期的所有文件

注: bt_16-08-30_00_22_03->2016年8月30日00:22:03

这里有两个解决方案

检查您的日期是否在文件名中

$date = '16-08-30';
foreach (new DirectoryIterator($dbfilepath) as $file) {
   if ($file->isFile()) {
     $thefilename = $file->getFilename();
     if (strripos($thefilename,".db")==true && strpos($thefilename, $date) !== false) {
       $thefiles[] = $thefilename;
     }
   }
 }
请注意,您不需要
$countdirfiles

或者使用glob,您可以使用regexp

$pattern = '*'.$date.'*.db';  // not sure about that regexp  
foreach (glob(pattern) as $filename) {
    echo "$filename \n";
}
这里有两个解决方案

检查您的日期是否在文件名中

$date = '16-08-30';
foreach (new DirectoryIterator($dbfilepath) as $file) {
   if ($file->isFile()) {
     $thefilename = $file->getFilename();
     if (strripos($thefilename,".db")==true && strpos($thefilename, $date) !== false) {
       $thefiles[] = $thefilename;
     }
   }
 }
请注意,您不需要
$countdirfiles

或者使用glob,您可以使用regexp

$pattern = '*'.$date.'*.db';  // not sure about that regexp  
foreach (glob(pattern) as $filename) {
    echo "$filename \n";
}
这是我的方法

为会话(或任何其他会话)指定日期。使其类似于文件名

if(isset($_SESSION['onedate'])){
  $btfifiletocomp = 'bt_' . date('y-m-d', strtotime($_SESSION['onedate'])) . '';      
}
现在检查文件名是否包含$btfifiletocomp

foreach (new DirectoryIterator($dbfilepath) as $file) {
   if ($file->isFile()) {
     $thefilename = $file->getFilename();
     if (strripos($thefilename,".db")==true) {
       if (0 === strpos($thefilename, $btfifiletocomp)){
         $thefiles[$countdirfiles] = $thefilename;
         $countdirfiles++;
       }
     }
   }
 }
这是我的方法

为会话(或任何其他会话)指定日期。使其类似于文件名

if(isset($_SESSION['onedate'])){
  $btfifiletocomp = 'bt_' . date('y-m-d', strtotime($_SESSION['onedate'])) . '';      
}
现在检查文件名是否包含$btfifiletocomp

foreach (new DirectoryIterator($dbfilepath) as $file) {
   if ($file->isFile()) {
     $thefilename = $file->getFilename();
     if (strripos($thefilename,".db")==true) {
       if (0 === strpos($thefilename, $btfifiletocomp)){
         $thefiles[$countdirfiles] = $thefilename;
         $countdirfiles++;
       }
     }
   }
 }

尝试使用
glob
尝试使用
glob