每个周日,我的日程网站上都会出现一些奇怪的事情。它显示下周的日期,而不是+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显示了下周的周一和周日(03-10-2016 \ 09-10-2016),但我想让它显示出来(10-10-2016 - 16-10-2016)
另外,有没有办法让周从星期一开始,而不是星期天?因为,在“本周”页面上,它已经显示了从26周-09周到2016年2月2日-2016年10周的数据,而不是19-09 2016年的数据25-10 2016年。
发布于 2016-09-25 14:19:18
strtotime函数错误发生在PHP5.5.31版本上。升级PHP版本或尝试下面的修补程序。
因此,我用if/‘ve语句修复了错误。
$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));
}由于错误只发生在周日,所以我只检查了if/ and语句的周日,而不是其他几天。如果今天不是星期天,它会运行其他代码,这是获取本周数据的正常方式。当今天不是星期天,而是一周中的另一天时,我会看代码是否100%起作用。
我只想向将来访问这个问题的人展示我对这个问题的解决方案。
https://stackoverflow.com/questions/39687248
复制相似问题