当前位置:网站首页>PHP has the problem of using strtotime to obtain time in months and months [original]

PHP has the problem of using strtotime to obtain time in months and months [original]

2022-06-26 04:19:00 Telkobe

Link to the original text

Use strtotime Time acquisition is used very frequently in daily work , However, there is a problem of months and months when we get the time , When the number of days is not extreme, we generally do not pay attention to , However, the day just happens to be the last day of the month, and unexpected situations may occur

for instance :

echo date('Y-m-d',strtotime('+1 month 2020-1-31'));

The result of the above code is :2020-03-02, I don't think this is the result we want , What we want is 2 On the last day of the month , So what is the reason for this , as a result of +1 month Words strtotime It is equivalent to operating the number of months , The result is 2020-2-31, however 2 The month did not 31 God ,2020 year 2 Month only 29 God , Two more days , So I am tired till next month , Then we get 2020-03-02 This result , How to solve it , Need to be in +1month Add a paragraph before :last day of;PS:php Version required >=5.3

echo date('Y-m-d',strtotime('last day of +1 month 2020-1-31'));

So we get the result we want :2020-02-29

原网站

版权声明
本文为[Telkobe]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202180535384860.html