Stomped
Stomped

Reputation: 2100

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

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.

Upvotes: 20

Views: 27445

Answers (6)

Jonathan Clark
Jonathan Clark

Reputation: 1260

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

Upvotes: 1

Winston
Winston

Reputation: 1805

strtotime("previous week Monday")

Monday of the previous week.

Upvotes: 8

Norman
Norman

Reputation: 79

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

Upvotes: 1

JRoquez
JRoquez

Reputation: 149

(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

Upvotes: 13

Vilx-
Vilx-

Reputation: 107052

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.

Upvotes: 2

ahmetunal
ahmetunal

Reputation: 3960

you can use strtotime for this kind of date issues

echo strtotime("last Monday");

Upvotes: 38

Related Questions