Home  >  Article  >  Web Front-end  >  How to get the time of the previous day or the next day in PHP

How to get the time of the previous day or the next day in PHP

小云云
小云云Original
2018-03-30 14:29:172218browse

This article mainly shares with you the method of getting the time of the previous day or the next day and a month in PHP, mainly in the form of code, and I hope it can help you.

//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;

$tomorrow = date('Y-m-d',strtotime("2018-03-29 + 1 day"));// 2018-03-29 加一天的日期 
echo $tomorrow; // 2018-03-30


You can also do this
date("Y-m-d",strtotime("-1 day"));//Get the time of the previous day directly

When it is January There is a problem when using this method date("Y-m-d", strtotime("-1 month")) to get the date of last month. The problem is that if there is a 30th or 31st in the current month, using this method to obtain the previous month will cause an error. For example, if you use this method to get the month of the previous month on January 30 or January 31, it will still be displayed as January. Therefore, it is an error to use this function to automatically obtain last month's records. Still a stupid solution:

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

The above is the detailed content of How to get the time of the previous day or the next day in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn