1
How to always pick the date of last Monday in PHP, For example today is Tuesday day 22/05 (Tuesday) and php picks the date 21/05 (Monday).
1
How to always pick the date of last Monday in PHP, For example today is Tuesday day 22/05 (Tuesday) and php picks the date 21/05 (Monday).
6
Just use the format date relative:
$date = new DateTime("last monday");
That would be the last Monday that passed.
Exit:
object(DateTime)#1 (3) {
["date"]=>
string(26) "2018-05-21 00:00:00.000000"
["timezone_type"]=>
int(3)
["timezone"]=>
string(16) "Europe/Amsterdam"
}
Code: https://3v4l.org/25tVY
It didn’t work, I used it the same way and it was wrong.
@Danielalencarknow what error has occurred because, as you can see in the link, it works in several versions of php (in all these: 5.6.0 - 5.6.30, hhvm-3.12.14 - 3.22.0, 7.0.0 - 7.2.4).
2
Utilize 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 function strtotime accepts a string, in the "US English date" format, and performs a parse on it transforming into a
timestamp
, making it possible to sum up dates, obtain specific days and numerous other functionalities.
Examples:
Last Monday strtotime('-1 Monday');
Last Monday strtotime("last Monday");
Next Thursday strtotime("next Thursday");
Next Thursday strtotime("+1 Thursday");
Second to last 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 one 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);
Browser other questions tagged php
You are not signed in. Login or sign up in order to post.
If today were Monday, it should show today’s date or the previous Mondays?
– Geraldão de Rívia