开发者

Find files based on a date in the filename

I have 100 text files in a directory.

The format of the filename is abcd_2011_04_20.txt

I need to read only TODAY's file and the past 7 days files . How do I go about it? EDIT 1 :

I already have a function dirTxt(dirname) which returns the name of the files as an array . How do I identify the current date's corresponding file?? and then get the previous 7 day's files??

EDIT 2 :

THE array returns the following

'graph.txt' 'graph1.txt' 'abcd_2011-开发者_如何学运维04-12.txt' 'abcd_2011-04-13.txt' 'abcd_2011-04-24.txt' 'abcd_2011-04-15.txt' 'abcd_2011-04-16.txt' 'abcd_2011-04-17.txt' 'abcd_2011-04-18.txt' 'abcd_2011-04-19.txt' 'abcd_2011-04-20.txt'


Example

$dt = time();   // today... or use $dt = strtotime('2010-04-20'); to set custom start date.
$past_days = 7; // number of past days
$filesindir = dirTxt('your_dir');

for ($i=0; $i<=$past_days; $i++) {
  $filename = 'abcd_' . date('Y_m_d', $dt) . '.txt';
  $files[] = $filename;
  $dt = strtotime('-1 day', $dt);
  } 

$files = array_intersect($filesindir, $files);

print_r($files);

Output (might be like this, depends of $filesindir array)

Array
(
    [0] => abcd_2011_04_21.txt
    [1] => abcd_2011_04_20.txt
    [2] => abcd_2011_04_18.txt
    [3] => abcd_2011_04_15.txt
)


I made a helper function, in case you need to use it again somewhere. If not, you could easily place its code inside of the loop's body.

function getFileName($unixTime) {
    return 'abcd_' . date('Y_m_j', $unixTime) . '.txt';
} 

$files = array();
foreach(range(0, 6) as $dayOffset) {
   $files[] = getFileName(strtotime('-' . $dayOffset . ' day'));
}

var_dump($files)

CodePad.

Output

array(7) {
  [0]=>
  string(19) "abcd_2011_04_21.txt"
  [1]=>
  string(19) "abcd_2011_04_20.txt"
  [2]=>
  string(19) "abcd_2011_04_19.txt"
  [3]=>
  string(19) "abcd_2011_04_18.txt"
  [4]=>
  string(19) "abcd_2011_04_17.txt"
  [5]=>
  string(19) "abcd_2011_04_16.txt"
  [6]=>
  string(19) "abcd_2011_04_15.txt"

Update

As for reading them, just loop...

foreach($files as $file) {
   if ( ! is_file($file)) {
       continue;
   }
   $contents = file_get_contents($file);
}


$filename = 'abcd_' . date('Y_m_d') . '.txt';
if (!file_exists($filename)) {
    die("File $filename does not exist");
}

$contents = file_get_contents($filename);

Use date('Y_m_d', strtotime('-2 days')) to get other dates.


Parse the date out of the filename and compare it to today's date.

PHP has string manipulation functions and date/time functions for this.


<?php
function isWithinLastSevenDays($str) {
   $pos = strpos($str, "_");
   if ($pos === FALSE)
      throw new Exception("Invalid filename format");

   $str = str_replace('_', '-', substr($str, $pos+1, strlen($str)-$pos-1-4));
   $d1 = new DateTime($str);
   $d2 = new DateTime();
   $d2->modify('-7 days'); // sub() only since PHP 5.3

   return ($d2 < $d1);
}

$str = "abcd_2011_04_20.txt";
var_dump(isWithinLastSevenDays($str));

$str = "abcd_2011_04_10.txt";
var_dump(isWithinLastSevenDays($str));

/*
Output:

bool(true)
bool(false)
*/
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜