Page 1 of 1

strtotime using string "first day of last week" not correct

Posted: 05 Nov 2020, 06:35
by Marius
Using the code:

Code: Select all

$from_date = date('Y-m-d 00:00:00', strtotime('first day of last week'));

echo $from_date;  // 2020-10-01 00:00:00
returns: "2020-10-01 00:00:00", when I expect it to return "2020-10-25 00:00:00".
Why is it returning the first day of the month?

How can I get the first day of last week (and also the last day of last week)?

strtotime using string "first day of last week" not correct

Posted: 05 Nov 2020, 09:09
by Admin
To get in php the "first day of last week", try strtotime() with the string: "last week last sunday".
To get the "last day of last week", use the phrase: "last day of last week".

Code: Select all

$first_day = date('Y-m-d 00:00:00', strtotime('last week last sunday'));
$last_day = date('Y-m-d 00:00:00', strtotime('last day of last week'));

echo $first_day;  // 2020-10-25 00:00:00
echo $last_day;  // 2020-11-30 00:00:00