比较日期和PHP文件名中的日期字符串


Compare dates against date strings in filenames in PHP

我试图通过pdf文件名的反向glob进行迭代,并找到在输入日期之前创建的pdf。(我已经成功地检查了一组文件名是否与输入的日期相等。)

我有下面的代码,我认为它应该可以工作,但是没有。

我假设日期/字符串转换是问题,但我在windows上运行php,我的调试工具是有限的。

有人能看到我在下面的代码做错了什么?

$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');
        }
    }
}

文件名如2007-07-11-wnr.pdf、2009-12-23-wnr.pdf和2013-04-02-wnr.pdf。

正如John Conde指出的那样,我的日期修改是错误的。

这是有效的代码。

$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 ) ) ;
        }
    }
}

谢谢约翰!