PHP Strtotime "Last Monday" If Today Is Monday

php strtotime last monday if today is monday?

How can I make it return today's date in that case?

pseudocode:

if (today == monday)
return today;
else
return strtotime(...);

Btw, this trick also could work:

strtotime('last monday', strtotime('tomorrow'));

Get previous and next monday date by given date

echo "Next Monday:". date('Y-m-d', strtotime('next monday', strtotime($givenDate)));
echo "Previous Monday:". date('Y-m-d', strtotime('previous monday', strtotime($givenDate)));

Get Last Monday - Sunday's dates: Is there a better way?

you can use strtotime for this kind of date issues

echo strtotime("last Monday");

php strtotime('this monday') shows date of coming week

Day names are documented as "Moves to the next day of this name" so, at first glance, the output looks correct. this doesn't add anything here and is probably just ignored.

Since you want to operate on weeks, you'll need to add week to the mix:

var_dump(date('r', strtotime('monday this week')));
var_dump(date('r', strtotime('saturday this week')));
var_dump(date('r', strtotime('sunday this week')));
// Edge case (today's day name):
var_dump(date('r', strtotime('friday this week')));
string(31) "Mon, 16 Aug 2021 00:00:00 +0200"
string(31) "Sat, 21 Aug 2021 00:00:00 +0200"
string(31) "Sun, 22 Aug 2021 00:00:00 +0200"
string(31) "Fri, 20 Aug 2021 00:00:00 +0200"

PHP - how to use $timestamp to check if today is Monday or 1st of the month?

Actually, you don't need timestamp variable because:

Exerpt from date function of php.net:

Returns a string formatted according to the given format string using
the given integer timestamp or the current time if no timestamp is
given. In other words, timestamp is optional and defaults to the value
of time().

if(date('j', $timestamp) === '1') 
echo "It is the first day of the month today\n";

if(date('D', $timestamp) === 'Mon')
echo "It is Monday today\n";

How to get last Monday from a specific date

You can use the class DateTime to create an object at the desired date, and then modify it :

$date = new DateTime('2020-10-11');
$date->modify('last monday');
echo $date->format('Y-m-d'); // output is 2020-10-05

As pointed jspit in comments, if the current date is monday and then if this date should be returned, a simple check can be added to avoid returning the wrong date :

$date = new DateTime('2020-10-11');
if ($date->format('N') != 1) // If the date isn't already monday
$date->modify('last monday');
echo $date->format('Y-m-d'); // output is 2020-10-05

PHP: Week starts on Monday, but monday this week on a Sunday gets Monday next week

As far as I can tell, this is a bug. I see no logical reason why strtotime('this week'); should return a future date. This is a pretty major bug. In my particular case, I had a leaderboard that showed the users with the most points since the beginning of the week. But on Sundays, it was empty because strtotime returned a timestamp for a future date. I was doubtful, because just I don't know how this could have gone unnoticed, but I couldn't find any other reports of this bug.

Thanks for all your time and help, folks.

PHP: how to get a day in last week?

You can use “this week” format:

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

eval.in demo

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



Related Topics



Leave a reply



Submit