StackOverflowNewbie
StackOverflowNewbie

Reputation: 40653

PHP: how to get a day in last week?

Is there an easy way to get last week's, say, Monday? If today is Tuesday, I do not want yesterday's Monday. Rather, I want the Monday 8 days ago (last week's Monday). Then I want that Monday's proceeding Sunday. Basically, I'm trying to get the date range for last week, Monday to Sunday.

This doesn't always work right:

date('Y-m-d', strtotime('last Monday')

Suggestions?

Upvotes: 0

Views: 336

Answers (2)

fusion3k
fusion3k

Reputation: 11689

You can use “this week” format:

$monday = strtotime( 'this week', strtotime( '7 days ago' ) );
$sunday = strtotime( '+ 6 days', $monday  );

3v4l.org demo

“this_week” returns monday of previous week, then — adding 6 days — you obtain the monday of relative week.

Upvotes: 3

Kayla
Kayla

Reputation: 475

The strtotime function accepts the current date as a parameter.

http://php.net/manual/en/function.strtotime.php

Just pass in strtotime('last Sunday') as the parameter to get a weekday of the last full week.

$beginning_of_week = strtotime('last Sunday');
$result = date('Y-m-d', strtotime('last Monday', $beginning_of_week));

echo $result;

Upvotes: 1

Related Questions