Reputation: 745
I am trying to iterate through a reverse glob of pdf file names and find the pdf created prior to the date entered. (I have already successfully checked the blob of file names against for one equal to the date entered.)
I have the following code, which I believe should work, but doesn't.
I am assuming the date/string conversions are the problem, but I'm running php on windows and my debugging tools are limited.
Can anyone see what I am doing wrong in the following code?
$d = "2007-07-11"
$firstdate = substr(array_slice(glob('*.pdf'), 0, 1), 0, 10);
echo "<br />" . $firstdate[0];
$counter = 0;
$date_to_check = strtotime(substr($d, 0, 10));
while ($counter < 1){
foreach(array_reverse(glob("*.pdf")) as $filename) {
if ((strtotime(substr($filename, 0, 10)) < $date_to_check) || ((substr($filename, 0 ,10) == $firstdate[0]))) {
$file_to_get = $filename;
$file_found = 1;
$counter = $counter + 1;
} else {
$date_to_check->modify('-1 day');
}
}
}
The filenames are like 2007-07-11-wnr.pdf, 2009-12-23-wnr.pdf and 2013-04-02-wnr.pdf.
Upvotes: 2
Views: 48
Reputation: 745
As John Conde pointed out my date modification was wrong.
This is the code that works.
$counter = 0;
$date_to_check = strtotime(substr($d, 0, 10));
while ($counter < 1){
foreach(array_reverse(glob("*.pdf")) as $filename) {
if ((strtotime(substr($filename, 0, 10)) < $date_to_check) || ((substr($filename, 0 ,10) == $firstdate[0]))) {
$file_to_get = $filename;
$file_found = 1;
$counter = $counter + 1;
} else {
$date_to_check = strtotime ( '+2 days' , strtotime ( $date_to_check ) ) ;
}
}
}
Thanks John!
Upvotes: 1