In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you an example analysis of the use of strtotime. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
Often people get confused when strtotime combines-1 month, + 1 month, next month, and then feel that this function is a little unreliable and often goes wrong. You will panic when you use it.
Today is 2018-07-31 to execute the code:
How does date ("Y-m-d", strtotime ("- 1 month")) output 2018-07-01?
Well, although this question looks confusing, from the internal logic, it is actually "right". Don't worry, ha, let me talk about it slowly:
Let's simulate the logic of dealing with this kind of thing within date:
Do-1 month first, so the current is 07-31, minus one is 06-31.
Then standardize the date, because June does not have 31, so it is like 2: 60 equals 3: 00. June 31 equals July 1. Is the logic "clear"? We can also manually verify the second step, such as:
Var_dump (date ("Y-m-d", strtotime ("2017-06-31"))
/ / output 2017-07-01, that is to say, as long as the last day of the month is involved, there may be this confusion, and we can easily verify other similar months to confirm this conclusion:
Var\ _ dump (date ("Y-m-d", strtotime ("- 1 month", strtotime ("2017-03-31")
/ / output 2017-03-03
Var\ _ dump (date ("Y-m-d", strtotime ("+ 1 month", strtotime ("2017-08-31")
/ / output 2017-10-01
Var\ _ dump (date ("Y-m-d", strtotime ("next month", strtotime ("2017-01-31")
/ / output 2017-03-03
Var\ _ dump (date ("Y-m-d", strtotime ("last month", strtotime ("2017-03-31")
/ / what should I do if I output 2017-03-03?
Starting with PHP5.3, date has added a series of correction phrases to clarify this problem, that is, "first day of" and "last day of", that is, you can limit the automatic "normalization" of date:
Var\ _ dump (date ("Y-m-d", strtotime ("last day of-1 month", strtotime ("2017-03-31")
/ / output 2017-02-28
Var\ _ dump (date ("Y-m-d", strtotime ("first day of + 1 month", strtotime ("2017-08-31")
/ / output 2017-09-01
Var\ _ dump (date ("Y-m-d", strtotime ("first day of next month", strtotime ("2017-01-31")
/ / output 2017-02-01
Var\ _ dump (date ("Y-m-d", strtotime ("last day of last month", strtotime ("2017-03-31")
/ / output 2017-02-28
If it's a version before 5.3.You can use things like mktime and ignore all the days, such as the 1st of each month, but it's not as elegant as using first day directly.
Now, if you figure out the internal principle, will you stop panicking?
The above is the analysis of the example of the use of strtotime shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.