[英]Strtotime function bug on Sunday
每个星期天,strtotime功能在我的日程安排网站上做了奇怪的事情。 它显示下周的日期而不是+2周。
我的代码:
date_default_timezone_set("Europe/Amsterdam");
$time = time();
//Check the date of Monday and Sunday from 2 weeks later.
$monday = date( 'd-m-Y', strtotime( '+1 week monday' ) );
$sunday = date( 'd-m-Y', strtotime( '+2 week sunday' ) );
//Check the weeknumber of this week and add 2, so that it shows the weeknumber of 2 weeks later.
$weekdata = date("W", strtotime('+1 day', $time));
$weeknumber = $weekdata + 2;
$Weeknumber
显示正确的信息:42,但$monday
和$sunday
显示下周的星期一和星期日(2016年10月3日| 2016年10月9日),但我想让它显示(10-10-2016 | 16- 10-2016)
另外,有没有办法让星期一星期一开始而不是星期日? 因为,在“本周”页面上,它已经显示了第26-09-2016周的数据 02-10-2016而不是19-09-2016 | 25-10-2016。
strtotime函数错误发生在PHP 5.5.31版上。 升级您的PHP版本或尝试下面的修复程序。
所以我用if / else语句修复了bug。
$time = time();
//Check the date of monday and sunday from this week.
//Strtotime() bug on PHP version 5.5.31. Bug report: https://bugs.php.net/bug.php?id=63740
if(date("l") == "Sunday"){
$monday = date( 'd-m-Y', strtotime( 'monday last week' ) );
$sunday = date( 'd-m-Y', strtotime( 'sunday last week' ) );
//Check the weeknumber of this week.
$weeknumber = date("W", strtotime('+0 day', $time));
} else {
$monday = date( 'd-m-Y', strtotime( 'monday this week' ) );
$sunday = date( 'd-m-Y', strtotime( 'sunday this week' ) );
//Check the weeknumber of this week.
$weeknumber = date("W", strtotime('+1 day', $time));
}
由于这个bug只发生在星期天,我只在周日检查if / else语句而不是其他日子。 如果今天不是星期天,它会运行else代码,这是获取本周数据的常规方法。 我会看到代码是否在今天不是星期日,而是一周中的另一天,100%工作。
我只是想向将来访问这个问题的人展示我对这个问题的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.