PHP 取前一天或后一天、一个月时间

原文链接: http://www.cnblogs.com/niejunlei/p/5318391.html

//获得当前时间    
//date()格式化时间返回String类型。     date("Y-m-d H:i:s")

$current_date = date(’Y-m-d’,time());    
//根据当前时间加一周后    
$weekLater = date(’Y-m-d’,strtotime("$current_date + 1 week"));    
echo $weekLate;    
// 2009-05-26 加一天的日期    
$tomorrow = date(’Y-m-d’,strtotime("2009-05-26 + 1 day"));    
echo $tomorrow; // 2009-05-27  

也可以这样 date("Y-m-d",strtotime("-1 day")) ;直接获得前一天时间



用此方法date(“Y-m-d”, strtotime(“-1 month”))得到上个月的日期时是有问题存在的。问题就出在当前月如果有30,31号时用此方法获取上月会出错。比如你在1月30号或1月31号时用此方法得到上月的月份会显示还是1月份。因此,采用这个函数自动获取上个月的记录则出错。还是笨办法解决:

if (date("n") == 1) {
    $tmpMonth = 12;
    $tmpYear = date ("Y") - 1;
}else{
    $tmpMonth = date ("n") - 1;
    $tmpYear = date ("Y");
}
$tmpDate = "$tmpYear-$tmpMonth-1";

转载于:https://www.cnblogs.com/niejunlei/p/5318391.html

猜你喜欢

转载自blog.csdn.net/weixin_30947043/article/details/94795283