PHP: Week starts on Monday, but “monday this week” on a Sunday gets Monday next week

后端 未结 10 1459
误落风尘
误落风尘 2020-12-09 14:43

Here\'s a summary of the issue: On Sundays, strtotime(\'this week\') returns the start of next week.

In PHP, the week seems to start on Monday. But, on

相关标签:
10条回答
  • 2020-12-09 15:34

    It's not ideal but this is what I resorted to using:

    if(date('N') == 7) { 
        $date = date('Y-m-d',strtotime('monday last week'));
    } else {
        $date = date('Y-m-d',strtotime('monday this week'));
    }
    
    0 讨论(0)
  • 2020-12-09 15:34
      I think the only problem with your coding is TimeZone.
    

    Solution:
    Set your own time Zone. Here is the example of my own time zone:

    Example

       date_default_timezone_set('Asia/Kolkata');
    
    
       Set the above line before calling any time function.
    

    Have a nice day.

    0 讨论(0)
  • 2020-12-09 15:35

    Based on Bryant answer :

    $first_week_date = date('d F Y', strtotime('next Monday -1 week', strtotime('this sunday')));
    $last_week_date = date('d F Y', strtotime('next Monday -1 week + 6 days', strtotime('this sunday')));
    
    0 讨论(0)
  • 2020-12-09 15:36

    As far as I can tell, this is a bug. I see no logical reason why strtotime('this week'); should return a future date. This is a pretty major bug. In my particular case, I had a leaderboard that showed the users with the most points since the beginning of the week. But on Sundays, it was empty because strtotime returned a timestamp for a future date. I was doubtful, because just I don't know how this could have gone unnoticed, but I couldn't find any other reports of this bug.

    Thanks for all your time and help, folks.

    0 讨论(0)
提交回复
热议问题