Home >Backend Development >PHP Tutorial >How to Get the Dates of Specific Weekdays in PHP?

How to Get the Dates of Specific Weekdays in PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-10-27 12:21:02839browse

How to Get the Dates of Specific Weekdays in PHP?

Get the Dates of Specific Weekdays

Want to retrieve the date stamps for specific weekdays, such as Monday, Tuesday, or any other day of the week? Look no further!

strtotime() to the Rescue

The magic function strtotime() comes into play here. It allows you to convert a human-readable date string into a Unix timestamp. So, simply use strtotime('next tuesday') to obtain the date stamp of the upcoming Tuesday.

Handling Dates Past the Current Week

What if the specified day has already passed this week? To account for this, you can check the week numbers. For instance:

<code class="php">$nextTuesday = strtotime('next tuesday');
$weekNo = date('W');
$weekNoNextTuesday = date('W', $nextTuesday);

if ($weekNoNextTuesday != $weekNo) {
    // The upcoming Tuesday is in the next week
}</code>

By comparing the week numbers, you can determine if the desired day has passed in the current week and adjust accordingly.

The above is the detailed content of How to Get the Dates of Specific Weekdays in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn