How to always get the date last Monday in PHP, for example today is Tuesday 22/05 (Tuesday) and php gets the date 21/05 (Monday).
How to always get the date last Monday in PHP, for example today is Tuesday 22/05 (Tuesday) and php gets the date 21/05 (Monday).
Just use the date relative format:
$date = new DateTime("last monday");
What would be the last Monday that passed.
Output:
object(DateTime)#1 (3) {
["date"]=>
string(26) "2018-05-21 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
Code: link
Use date to format the date according to strtotime informed
echo date('d/m/Y',strtotime('-1 Monday')); // 21/05/2018
echo date('d-m-Y',strtotime('-1 Monday')); // 21-05-2018
echo date('d/m',strtotime('-1 Monday')); // 21-05
echo date('d-m',strtotime('-1 Monday')); // 21-05
The strtotime function accepts a string, in the format "US English date", and parse it into a
timestamp
, making it possible to add dates, get specific days, and count other features.
Examples:
Last Monday strtotime('-1 Monday');
Last Monday strtotime("last Monday");
Next fifth strtotime("next Thursday");
Next fifth strtotime("+1 Thursday");
Penultimate Monday strtotime('-2 Monday');
Next Monday strtotime('+1 Monday');
Catch the time now strtotime("now");
Using a textual date strtotime("10 September 2017");
Add a day strtotime("+1 day");
Add a week strtotime("+1 week");
Add a week, two days, four hours, and two seconds strtotime("+1 week 2 days 4 hours 2 seconds");
Pick today's date and add 10 days
$now = strtotime("now");
strtotime("+10 day",$now);