PHP Date Operation Tips: How to easily get the day of the week for a specified date?
In PHP development, operating dates is a common requirement. Sometimes we need to get the day of the week for a specified date in order to perform corresponding logical processing. This article will introduce some simple methods to help you easily get the day of the week for a specified date. The following is a specific code example:
Use the date() function combined with the "w" parameter
$date = "2022-08-15"; // Specified date $day_of_week = date('w', strtotime($date)); // Get the day of the week, the return value is 0 (Sunday) to 6 (Saturday) $week_day_array = array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"); echo "The specified date {$date} is: {$week_day_array[$day_of_week]}";
Use the date() function combined with the "l" parameter
$date = "2022-08-15"; // The specified date $day_of_week = date('l', strtotime($date)); // Get the English name of the day of the week echo "The specified date {$date} is: {$day_of_week}";
Use the date() function combined with the "N" parameter
$date = "2022- 08-15"; // specified date $day_of_week = date('N', strtotime($date)); // Get the day of the week, the return value is 1 (Monday) to 7 (Sunday) $week_day_array = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"); echo "The specified date {$date} is: {$week_day_array[$day_of_week - 1]}";
Through the above methods, you can easily get the day of the week for the specified date , and choose the appropriate method to use according to your needs. I hope these tips can help you handle date operations more conveniently in PHP development!
The above is the detailed content of PHP date operation tips: How to easily get the day of the week for a specified date?. For more information, please follow other related articles on the PHP Chinese website!