PHP Strtotime If there is a bug

Source: Internet
Author: User
$beginMon=strtotime("-1 week Monday");$endMon=strtotime("-1 week Tuesday")-1;echo date("Y-m-d H:i:s", $beginMon);echo('
');echo date("Y-m-d H:i:s", $endMon);echo("
");//上面是获取本周一的开始与结束时间戳//结果如下://2015-01-05 00:00:00(错误)//2014-12-29 23:59:59(正确)$beginSun=strtotime("+0 week Sunday");$endSun=strtotime("+1 week Monday")-1;echo date("Y-m-d H:i:s", $beginSun)."
";echo date("Y-m-d H:i:s",$endSun)."
";//上面获取的是本周末的开始与结束时间戳//结果如下://2015-01-04 00:00:00//2015-01-11 23:59:59(错误)//以上案例都是在今天测试(非周末 因为周末)

Reply content:

$beginMon=strtotime("-1 week Monday");$endMon=strtotime("-1 week Tuesday")-1;echo date("Y-m-d H:i:s", $beginMon);echo('
');echo date("Y-m-d H:i:s", $endMon);echo("
");//上面是获取本周一的开始与结束时间戳//结果如下://2015-01-05 00:00:00(错误)//2014-12-29 23:59:59(正确)$beginSun=strtotime("+0 week Sunday");$endSun=strtotime("+1 week Monday")-1;echo date("Y-m-d H:i:s", $beginSun)."
";echo date("Y-m-d H:i:s",$endSun)."
";//上面获取的是本周末的开始与结束时间戳//结果如下://2015-01-04 00:00:00//2015-01-11 23:59:59(错误)//以上案例都是在今天测试(非周末 因为周末)

You can read the relevant implementation source to understand why there are such results: php_date.c and parse_date.c.

You can also read the official documents and the usual time processing references to see how to practice.

Seven days per week, ' Sunday ' | ' Monday ' | ' Tuesday ' | ' Wednesday ' | ' Thursday ' | ' Friday ' | ' Saturday ' | ' Sun ' | ' Mon ' | ' Tue ' | ' Wed ' | ' Thu ' | ' Fri ' | ' Sat ' | ' Sun ' should be based on the current time to push back 7 * 24 hours to consider. In this case, if you want to refer specifically to a Monday of the week, you can use the strtotime("-1 week Monday this week"); other days, similarly, to specify which natural week (in this case, the this week Natural Week), do not allow the system to handle itself by its own default logic.

Note: The answer to the question is Tuesday.

This is not Bug,php's Official Handbook, which actually mentions similar situations. In some cases strtotime ("1 months") is not next month, but next month. You can flip through the documentation manual. You know why you're dealing with it.

  • 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.