Get Last Monday - Sunday's dates: Is there a better way?
Asked Answered
W

6

20

I'm preparing a query for mySQL to grab record from the previous week, but I have to treat weeks as Monday - Sunday. I had originally done this:

WHERE YEARWEEK(contactDate) = YEARWEEK(DATE_SUB(CURDATE(),INTERVAL 7 DAY))

to discover that mySQL treats weeks as Sunday - Monday. So instead I'm parsing getting the begin & end dates in php like this:

$i = 0; 
while(date('D',mktime(0,0,0,date('m'), date('d')-$i, date('y'))) != "Mon") { 
  $i++; 
}

$start_date = date('Y-n-j', mktime(0,0,0,date('m'), date('d')-($i+7), date('y')));
$end_date  = date('Y-n-j', mktime(0,0,0,date('m'), date('d')-($i+1), date('y')));

This works - it gets the current week's date for monday (walking backwards until a monday is hit) then calculates the previous week's dates based on that date.

My question is: Is there a better way to do this? Just seems sloppy, and I expect someone out there can give me a cleaner way to do it - or perhaps not because I need Monday - Sunday weeks.

Edit

Apparently, there is:

$start = date('Y-m-d',strtotime('last monday -7 days'));
$end   = date('Y-m-d',strtotime('last monday -1 days'));

That's about a million times more readable. Thank you.

Winglet answered 9/3, 2010 at 21:9 Comment(0)
S
37

you can use strtotime for this kind of date issues

echo strtotime("last Monday");
Shred answered 9/3, 2010 at 21:12 Comment(7)
Elegant! I had forgotten about this.Arroyo
echo date('Y-m-d',strtotime('last monday -7 days')); Mind boggling.Winglet
Mind boggling is right! Awesome! strftime('%Y-%m-%d', strtotime("last week Monday")) OR try: strftime('%Y-%m-%d', strtotime("first day of last month"));Amylopectin
What if today's Monday? What will it return?Upbraid
@AJ it will return Last Monday, meaning 7 days before.Shred
@marvin Did you wait till today to test it? ;-)Upbraid
it should be "previous week Monday", Last Monday will return this Monday for current week.Interdigitate
T
13

(complementing on marvin and Stomped ) Also you can use it this way

echo date('Y-m-d',strtotime('-1 Monday')); //last Monday
echo date('Y-m-d',strtotime('-2 Monday')); //two Mondays ago
echo date('Y-m-d',strtotime('+1 Monday')); //next Monday
Threepiece answered 31/7, 2012 at 18:42 Comment(0)
K
8
strtotime("previous week Monday")

Monday of the previous week.

Kingsly answered 22/1, 2016 at 8:56 Comment(2)
exactly what i've been looking for... #awesomeCleavland
It's more correct answer instead "-1 Monday" and "Last Monday" which return monday of current weekThumbsdown
A
2

Marvin's answer is really elegant, although if you really wanted to go for performance you could do it with a little arithmetic. You could derive a formula/method to convert an arbitrary date to "days since some starting point" (probably 01.01.0000) and then the rest of the operations would be easy with that. Getting the day of week from such a number is as simple as subtracting and getting the remainder of a division.

Actually, PHP had a Date class in its PEAR library which did exactly this.

Arroyo answered 9/3, 2010 at 21:15 Comment(0)
B
1

I have to point out for all the readers here there is a big issue with marvin's answer

Here is the catch The "last Monday" function will return date the "latest" Monday.It will be explained like this , if today is Monday, then it will return the date of "LAST WEEK" Monday, however if today is not Monday, it will return "THIS WEEK" Monday The Question is request to return "Last Week" Monday. Therefore the result will be incorrect if today is not Monday. I have solved the issue and wrapped in my Date Time Helper It will be only one line after you "INCLUDE" the class

$lastWeekMonday = Model_DTHpr::getLastWeekMonday();

Check Here

https://github.com/normandqq/Date-Time-Helper

Boysenberry answered 3/9, 2013 at 3:24 Comment(1)
I found that, too. However, in my case, I wanted to ensure I did get last week Monday even if I ran it in a Tues. I found you can just do this though: strftime('%Y-%m-%d', strtotime("last week Monday"))Amylopectin
H
0

If you want an object orientated way, you can do:

$monday = new \DateTime('last monday'); // or ('-1 monday')
$monday = $monday->format('Y-m-d');

$sunday = new \DateTime('last sunday');
$sunday = $monday->format('Y-m-d');
Hancock answered 29/7 at 12:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.