Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

datetime

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
like image 904
tvanc Avatar asked Dec 10 '12 09:12

tvanc


People also ask

How can I get a week start and end in php?

getStartAndEndDate($week, $year); output: $return[0] = $firstDay; $return[1] = $lastDay; The return value will be something like an array in which the first entry is the week starting date and the second being the ending date.

How do I get the start of the week in php?

Use strtotime() function to get the first day of week using PHP. This function returns the default time variable timestamp and then use date() function to convert timestamp date into understandable date. strtotime() Function: The strtotime() function returns the result in timestamp by parsing the time string.

How can I get Monday of current week in php?

Try this. echo date('Y-m-d', strtotime('last monday', strtotime('next monday'))); It will return current date if today is monday, and will return last monday otherwise.

How do you determine the first day of the week?

To get the first day of the week, we subtract the day of the week from the day of the month. If the day of the week is Sunday, we subtract 6 to get Monday, if it is any other day we add 1 , because the getDay method returns a zero-based value.


4 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.

like image 131
tvanc Avatar answered Sep 21 '22 23:09

tvanc


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')); 
like image 43
Bryant Avatar answered Sep 25 '22 23:09

Bryant


Here is how you can get Monday of current week:

echo date("Y-m-d", strtotime(date('o-\\WW')));
like image 39
Vadim Guzev Avatar answered Sep 22 '22 23:09

Vadim Guzev


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'));
}
like image 32
sebtucknott Avatar answered Sep 22 '22 23:09

sebtucknott