Explanation of the principle of time function strtotime () function in PHP

Source: Internet
Author: User

This article introduces to you about PHP in the time function strtotime () function of the principle of explanation, there is a certain reference value, the need for friends can refer to, I hope to help you.

Often people are strtotime combined -1 month, +1 month, next month when confused, and then will feel that this function is a bit less reliable, and the problem. When you use it, you're going to panic.

This is not, someone has just asked me:

Today is 2018-07-31 execution code: date ("y-m-d", Strtotime ("-1 Month"))//How output is 2018-07-01?

OK, although this question seems to be confusing, but from the internal logic, actually is "right", you do not worry, let me slowly say:

Let's simulate the processing logic for this kind of thing within the date:

1, first do -1 month, then the current is 07-31, minus one is 06-31.

2, then do date normalization, because June no 31st, so it is like 2 points 60 equals 3 points, June 31 is equal to 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, as long as the last day of the month is involved, it can be confusing, and it is easy to verify similar other months to confirm this conclusion:

Var_dump (Date ("Y-m-d", Strtotime ("-1 month", Strtotime ("2017-03-31")));//Output 2017-03-03var_dump (date ("y-m-d", Strtotime ("+1 Month", Strtotime ("2017-08-31")));//Output 2017-10-01var_dump (date ("Y-m-d", Strtotime ("next month", Strtotime ("2017-01-31")));//Output 2017-03-03var_dump (date ("Y-m-d", Strtotime ("Last month", Strtotime ("2017-03-31"))) );//Output 2017-03-03

What about that?

Starting with PHP5.3, the date adds a series of correction phrases to clarify the question, that is, "first day of" and "Last day of", that is, you can limit the date to not automatically "normalize":

Var_dump (Date ("Y-m-d", Strtotime ("Last day of-1 Month", Strtotime ("2017-03-31")));//Output 2017-02-28var_dump (date (" Y-m-d ", Strtotime (" First day of +1 month ", Strtotime (" 2017-08-31 ")));////output 2017-09-01var_dump (date (" y-m-d ", Strtotime ("First day of next month", Strtotime ("2017-01-31"))),////output 2017-02-01var_dump (date ("Y-m-d", Strtotime (" Last day of the last month ", Strtotime (" 2017-03-31 "))));////output 2017-02-28

What if it was a version before 5.3 (someone else used it?), you can use the mktime and so on, all the days are ignored, for example, are limited to the number of 1th per month on it, but it is not as direct as the first day to be more elegant.

Now, figuring out the internal principle, is it not panic?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.