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

php get first day of week from week number
print days of the week in php
php week start from monday
php week number
php get monday of current week
php first day of next month
php date
php get first day of month

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

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.

PHP: Week starts on Monday, but "monday this week" on a Sunday , 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. Week numbers in PHP How to get the week number from a date. To get the Unix timestamp representing the start of the week (Monday at midnight), use strtotime

Here is how you can get Monday of current week:

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

Relative Formats - Manual, "number week dayname" will first add the number of weeks, but does not advance to since Calendars typically start on monday, this is the date you might want,  ISO-8601 week number of year, weeks starting on Monday: Example: 42 (the 42nd week in the year) Month-----F: A full textual representation of a month, such as January or March: January through December: m: Numeric representation of a month, with leading zeros: 01 through 12: M: A short textual representation of a month, three letters: Jan through Dec: n

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

Change the week start day from Monday to Saturday, I think it's not related to WordPress but maybe it can help you: <?php ini_set('date​.timezone', 'Europe/Lisbon'); ini_set('intl.default_locale',  WEEK() function. MySQL WEEK() returns the week number for a given date. The argument allows the user to specify whether the week starts on Sunday or Monday and whether the return value should be in the range from 0 to 53 or from 1 to 53. If no argument is included with the function, it returns the default week format.

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

Reliable PHP function to return Monday for week · GitHub, <?php. /**. * Find the starting Monday for the given week (or for the current week if no date is passed) making strtotime('Monday this week') on a Sunday return the adjacent Monday. * instead of the very simple, but does what it must  It will be nice to mention ISO 8601 as well: “the ISO week-numbering year starts at the first day (Monday) of week 01 and ends at the Sunday”.

  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.

How to display the start and end date of the current week in PHP , I have learned all theory parts of PHP but don't know how to practice. Where should I You can use the following code snippet to find the start and end date in current week ==>. <?php. $monday $monday+7*86400 : $monday;. $​sunday  To get the dates from Monday to Friday of the last week, we need to calculate the difference between the previous monday from the strtotime function and the current date, if the difference is less than 7 that means the monday is within the current week. If it is the monday of the current week, then we can apply the previous monday one more time

PHP: Week starts on Monday, but “monday this week” on a Sunday , 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  Defines the day of week deemed to start the week. This affects the behavior of fields that depend on the concept of week start and end such as IntlCalendar::FIELD_WEEK_OF_YEAR and IntlCalendar::FIELD_YEAR_WOY.

PHP Get First and Last Day of Week by Week Number, 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  [2013-01-23 16:04 UTC] googleguy@php.net I've actually recently updated the documentation about strtotime in regard to this very behavior. See Bug #52143.The problem is that prior to PHP 5.3.0 the relative time formats "this week", "next week", "previous week" were taken to mean a 7 day period relative to the current time.

Calculating past and future dates < PHP, This method calculates the beginning and ending of a week given the year and week number. ISO-8601 specifies the first day of the week as “Monday”. I'm not saying that Sunday doesn't happen twice a week… but  [Solved] Convert date to week number, week starts Monday by jack111 » Fri May 01, 2009 11:48 am Have the data below need to convert it such that it displays week number.

Comments
  • Perhaps there are timezone shenanigans afoot? Can you try this with a DateTime that's been tied to a local DateTimeZone? Also, have you replaced your server's flux capacitor? They wear out after a while. :p
  • I think first day of week for PHP date function is Sunday: w | Numeric representation of the day of the week | 0 (for Sunday) through 6 (for Saturday)
  • @SalmanA, I thought that too, but I kept noticing as I was working on the site that getting the start of the week returned Monday. For instance, date('Y-m-d', strtotime('this week'); returns Monday's date. And the capital W under the week section of the $format parameter says the week starts on Monday: php.net/manual/en/function.date.php italic_W | ISO-8601 week number of year, weeks starting on Monday (added in PHP 4.1.0)_italic I haven't been able to find any statement to prove for certain that PHP's weeks start on Monday except for unsubstantiated claims in forum and blog posts.
  • @Charles, I tried this and got the same result: $date = new DateTime(); $date->setTimezone(new DateTimeZone('America/Los_Angeles')); $date->setTimestamp(strtotime('last sunday')); echo $date->format('Y-m-d'); Also, I checked, and the quasitronic fluxication rate on my server should be... unlimited. So, I don't think that's the problem.
  • I've posted a bug report here: bugs.php.net/bug.php?id=63740 Feel free to add your feedback.
  • A little bit late, I had the same problem... You can simply fix this by doing strtotime('next Monday -1 week'); This will always give you the Monday of the current week ;)
  • @FrederikVoordeckers great idea! Didn't work for me like that but this did: $this_date_start = new DateTime('next monday'); $this_date_start ->sub(new DateInterval('P7D'));
  • This works great @FrederikVoordeckers. But on sunday I must use strtotime('sunday this week').
  • On a Monday that will return the date of last week's Monday. echo date('Y-m-d', strtotime(date('o-\\WW'), strtotime('2013-02-18'))); 2013-02-18 is a Monday. The above code echos 2013-02-11, which is the previous Monday. Regardless, it doesn't explain why PHP should return a date in the future when I supply "this week" to strtotime
  • I've just used this solution and strtotime(date('o-\\WW')) returns the same monday if the base timestamp is a monday too. Which version of PHP are you using?
  • @turibe, your strtotime is in the wrong place, it should be: echo date('Y-m-d', strtotime(date('o-\\WW', strtotime('2013-02-18'))));
  • That would work if I was trying to get the date of last Monday, but what I'm trying to do is get the date of the most recent Monday, including today if today is Monday. date('Y-m-d', strtotime('this week'); returns Monday's date, regardless of what day you run that code on or supply as strtotime's second parameter.
  • Which is to say I want to get the start of the week. But if I make the attempt to get the start of the week during a Sunday, PHP returns a date in the future: The coming Monday.