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

后端 未结 10 1458
误落风尘
误落风尘 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:18

    This is for thos looking for a friendly solution that works with any day.

    function getWeekStart($week_start_day = "Monday") {
        $week_days = array("Sunday"=>0,"Monday"=>1,"Tuesday"=>2,"Wednesday"=>3,"Thursday"=>4,"Friday"=>5,"Saturday"=>6,);
    
        if(!isset($week_days[$week_start_day])) {
            return false;
        } else {
            $start_day = $week_days[$week_start_day];
    
            $today = date("w");
            $one_day = (60 * 60 * 24);
    
            if($today < $start_day) {
                $days_difference = 7 - ($start_day - $today);
            } else {
                $days_difference = ($today - $start_day);
            }
    
            $week_starts = strtotime(date("Y-m-d 00:00:00")) - ($one_day * $days_difference);
    
            return $week_starts;
        }
    }
    
    //Test: If today is Monday, it will return today's date
    echo date("Y-m-d H:i:s", getWeekStart("Monday"));
    
    0 讨论(0)
  • 2020-12-09 15:20

    I think instead of trying

    echo date('Y-m-d', strtotime('monday this week', strtotime('last sunday')));
    

    you should try

    echo date('Y-m-d', strtotime('monday last week'));
    
    0 讨论(0)
  • 2020-12-09 15:20

    Try this code

    // set current date
    $date = date("m/d/Y");
    $ts = strtotime($date); // also $ts = time();
    
    // find the year and the current week
    $year = date('o', $ts);
    $week = date('W', $ts);
    // print week for the current date
    $i = 1; // 1 denotes the first day of week
    
    $ts = strtotime($year.'W'.$week.$i);
    echo $day = date("l", $ts); // generate the name of day
    echo "<br>";
    echo $day = date("Y-m-d", $ts); // generate the date
    

    You will get the the date of current week, whether you are on monday you will get the date of that monday.

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

    If you want the most recent monday:

    function mostRecentMonday(){
      if(date("w") == 1){
       return strtotime("midnight today");
      } else {
       return strtotime("last monday");
      }
    }
    

    Easy to modify to use DateTime, or, to even specify a different date to use as the base.

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

    Here is how you can get Monday of current week:

    echo date("Y-m-d", strtotime(date('o-\\WW')));
    
    0 讨论(0)
  • 2020-12-09 15:29

    This answer is late, but it's something that I've been struggling with. Every solution I've tried so far has malfunctioned for one reason or another. This is what I ended up with that worked for me. (though it may be look pretty, it at least works).

    $thisMonday =  strtotime('next Monday -1 week', strtotime('this sunday'));
    
    0 讨论(0)
提交回复
热议问题