In PHP, you can get the current date using the date function, which is one of the most commonly used functions for handling dates and times.
$currentDate = date('Y-m-d');
The code above stores the current date in the format "Year-Month-Day" into the variable $currentDate.
New Year's Day is on January 1st each year, so we just need to get the current year and append 01-01 to it.
$newYearDate = date('Y') . '-01-01';
This gives us the full date of New Year's Day for the current year.
To find out the exact weekday of New Year's Day, you can use strtotime to convert the date to a timestamp, then combine it with the formatting capability of the date function.
$weekday = date('l', strtotime($newYearDate));
Here, 'l' outputs the full English name of the weekday (such as Monday, Tuesday, etc.).
The final step is to output the result using the echo function.
echo 'New Year's Day is on: ' . $weekday;
The browser will display something like "New Year's Day is on: Wednesday".
Below is the complete PHP code. Copy and run it to see the result:
$currentDate = date('Y-m-d');
$newYearDate = date('Y') . '-01-01';
$weekday = date('l', strtotime($newYearDate));
echo 'New Year's Day is on: ' . $weekday;
With the code provided in this article, you can easily determine the weekday of New Year's Day each year. The date and strtotime functions in PHP are very useful for handling dates and times, and they are especially helpful for everyday development tasks involving time formatting and conversions.