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