php get `date("Ymd")-1`


php get `date("Ymd")-1`

我存储了一些带有日期的error_log作为它们的名称。如20120827.txt

现在,我为查看error_log编写了一些php脚本。

我需要做一些判断:

if(file_exists(date("Ymd").'.txt')){ 
   $html = file_get_contents(date("Ymd").'.txt');
}else{
   $html = file_get_contents((date("Ymd")-1).'.txt');//load yesterday's `error_log`.
}

但这不适合像20120801.txt这样的日期,如何做date("Ymd")-1

我试过date("Ymd", (strtotime($date) . " -1 day"));// return:19700101

如果你想要昨天的日期,你可以做

date("Ymd", time() - 3600*24);

或者您可以使用DateTime对象:

$date = new DateTime();
$date->modify("-1 day");
echo $date->format("Ymd");

试试这个。。。最简单的一个。。。

date('Ymd',strtotime( "yesterday" ));

您可以使用PHP的mktime来获得非常精确的日期:

date('Ymd', mktime(0, 0, 0, date('m'), date('d') - 1, date('Y')));

您需要像这样使用strtotime

date('Ymd', strtotime('-1 day'));

这是一个带有输出的粘贴:http://codepad.org/iD7M9VN7