Home  >  Article  >  Backend Development  >  How to calculate today's day of the previous month using PHP_PHP Tutorial

How to calculate today's day of the previous month using PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:10:27908browse

One day, I encountered a problem. I asked for today's date a month ago. At first, we used the strtotime(”-1 month”) function to evaluate, and found that there was a problem. The calculation results of months with different month lengths were incorrect. For example: 2011-03-31, the result is 2011-03-03. Let’s not investigate the problem first, let’s look at how to solve it. At this time, I remembered that there is a mktime function in PHP, so I wrote the following code:

Copy the code The code is as follows:

echo date("Y-m-d H:i:s", mktime(date("G", $time), date("i", $time),
date("s", $time), date(" n", $time) - 1, date("j", $time), date("Y", $time)));

When executed, the result is found and the result of strtotime It's the same.
Still based on this function, since the month cannot be directly manipulated, we start with the day to get the previous month, and then use date to splice the data. The following code:
Copy code The code is as follows:

$time = strtotime("2011-03-31");
/**
* Calculate today’s date in the previous month
* @param type $time
* @return type
*/
function last_month_today($time) {
$last_month_time = mktime(date("G", $time), date("i", $time),
date("s", $time), date("n", $time), - 1, date("Y", $time));
return date(date("Y-m", $last_month_time) . "-d H:i:s", $time);
}
echo last_month_today($time);

But there is another problem at this time, which does not exist What should I do with a date like 2011-02-31? The current requirement is to display the last day of the month for such a date. The following code:
Copy code The code is as follows:

$time = strtotime("2011-03-31");
/**
* Calculate today’s day of the previous month. If there is no today in the previous month, return the last day of the previous month.
* @param type $time
* @return type
*/
function last_month_today($time){
$last_month_time = mktime(date("G", $time), date("i", $time),
            date("s", $time), date("n", $time), 0, date("Y", $time));
   $last_month_t = date("t", $last_month_time);
if ($last_month_t < date("j", $time)) {
return date("Y-m-t H:i:s", $last_month_time);
}
return date(date ("Y-m", $last_month_time) . "-d", $time);
}
echo last_month_today($time);

One thing to note here: date("Y-m ", $last_month_time) . "-d" this code. In the process of writing code, if you write "Y-" . date("m", $last_month_time) . "-d", there will be a problem with the time span of the year. I discovered this while writing this article.
In addition to this method, you can also calculate the year, month and day first and then splice the string. Here is a pure string operation.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327118.htmlTechArticleOne day, I encountered a problem, please ask for today's date a month ago. At first, we used the strtotime(”-1 month”) function to evaluate and found that there was a problem. The calculation results of months with different month lengths...
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