首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >周日的Strtotime函数错误

周日的Strtotime函数错误
EN

Stack Overflow用户
提问于 2016-09-25 13:16:41
回答 1查看 268关注 0票数 1

每个周日,我的日程网站上都会出现一些奇怪的事情。它显示下周的日期,而不是+2周。

我的代码:

代码语言:javascript
复制
    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年。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-25 14:19:18

strtotime函数错误发生在PHP5.5.31版本上。升级PHP版本或尝试下面的修补程序。

因此,我用if/‘ve语句修复了错误。

代码语言:javascript
复制
    $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%起作用。

我只想向将来访问这个问题的人展示我对这个问题的解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39687248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档