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

Discuss coding issues, and scripts related to PHP and MySQL.
Marius
Posts: 107

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

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)?

Admin Posts: 805
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