I'm going to go, did I find a bug in PHP? Calculate the last day of next month's problem!

Source: Internet
Author: User
Tags echo date
Words not much to say, directly on the code:
$BeginDate = "2015-05-31";//New Gets the last 1 days of the specified month echo date ("Y-m-t", Strtotime ("+1 months", Strtotime ($BeginDate)));


Based on experience, the results should be: 2015-06-30

But the actual operation result is not, everybody please try, and ask everybody this is why ~ ~ ~ knocked Me Out

Another: If the start time is set to 2015-05-30 the calculation result is correct


Reply to discussion (solution)

$BeginDate = "2015-05-31"; Echo date ("Y-m-d T", Strtotime ("+1 months $BeginDate"));
2015-07-01 on 31

What's the problem?
May 31 plus One months (June is 30 days) so that's July 1, the last day of July is 31st?
Looks like something's wrong with your experience.

Oh, study.
I thought +1 months was the next month of that month.

If so I think PHP has a bug, this situation should not go into 1, but should return to the last day of June

You understand the error!!
Get another syntax for the last day of the next month.

Oh, plus one months, it must be July 1.
If according to your logic, plus one day that's not equal to May 31?

Bug You're not dealing with the logic.

I usually use the next month, 1st, minus another 1 days.

I changed the algorithm, is a good solution to this problem
Although there are many ways, I still insist that this is a PHP bug, as the first floor said, May has 31 days so +1 months is June 31, but June has 30 days, so the result is July 1, then this strtotime +1 months is bug!

The way to change yourself is:

$begindate = "2015-05-31"; $enddate =date ("y-m-01", Strtotime ($begindate));//Get $enddate=date at the beginning of the month ("Y-m-t 23:59:59", Strtotime ("$enddate +1 Month");//month plus 1 month then calculate the last day of the month

$BeginDate = "2015-05-31"; Echo date ("Y-m-d T", Strtotime ("+1 months $BeginDate"));
2015-07-01 on 31

What's the problem?
May 31 plus One months (June is 30 days) so that's July 1, the last day of July is 31st?
Looks like something's wrong with your experience.



How is PHP's plus one months calculated? May 31 plus one month is the simple May +1=6 month 31st? Then June 31 does not exist so the result is July 31?

If May 31 plus one day is 32 days, but 32 days does not exist is June 1?

I understand that, right?

Yes
But please note: This is not a PHP rule, but a rule of time in English.

  • Related Article

    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.