Home » Php » datetime – PHP: Week starts on Monday, but "monday this week" on a Sunday gets Monday next week

datetime – PHP: Week starts on Monday, but "monday this week" on a Sunday gets Monday next week

Posted by: admin April 23, 2020 Leave a comment

Questions:

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 any day except Sunday, this code

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

Outputs the date of this week’s Monday, when it seems like it should be outputting last weeks Monday. It seems like, in this case, PHP is treating both Sunday and Monday as the the start of the week. It’s now Monday, Dec 10, 2012, or 2012-12-10. date('Y-m-d', strtotime('sunday last week')) returns 2012-12-09 – yesterday.

Is this a bug, or am I missing something? It seems like a bug this obvious should be fairly well known, but I can’t find anything about it. Is the only way to get the start of the week to use some special handling for Sundays?

$week_offset = (int) 'sunday' == date('l');
$week_start  = strtotime("-$week_offset monday"); // 1 or 0 Mondays ago
How to&Answers:

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.

Answer:

Here is how you can get Monday of current week:

echo date("Y-m-d", strtotime(date('o-\WW')));

Answer:

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'));

Answer:

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'));
}

Answer:

  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.

Answer:

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'));

Answer:

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.

Answer:

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.

Answer:

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')));

Answer:

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"));