00 Votes

PHP: Identify date of last and next Monday

Question by Axuter | 2012-03-13 at 23:40

I need a function in PHP, that outputs the date of the last and the next Monday. I was thinking something about that topic, but I had no idea, because I do not know how to determine a weekday in PHP. The whole seems to be quite complicated, as my previous research on the Internet has shown. I found some huge and long calendar calculations, which I find a bit too long for the problem.

Has anyone of you a simple approach and can help me?

ReplyPositiveNegative
0Best Answer0 Votes

Fortunately, PHP already comes by itself with all that we need to solve the problem. For this purpose, here is the code:

$tlastmonday = strtotime("last Monday");
$tnextmonday = strtotime("next Monday");
 
// Output
echo ' Last Mondy: ';
echo date("d.m.Y", $tlastmonday);
echo ' Next Monday: ';
echo date("d.m.Y", $tnextmonday);

We can simply pass the parameter "last Monday" or "next Monday" to the PHP function strtotime() and it gives us the date we have searched for. The lines below output the date for us.

If you need a different day, you can simply use the names of the weekdays and already you got it!

Similarly, we can also operate in the same way with using "last week" or "next week", "month" or "year", to get the date of the last week, the last months or year.
2012-03-15 at 16:47

ReplyPositive Negative
Reply

Related Topics

PHP: Current Date and Time

Tutorial | 0 Comments

PHP: Determine Week Number

Tutorial | 0 Comments

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.