Home >Backend Development >PHP Tutorial >PHP date operation tips: How to easily get the day of the week for a specified date?

PHP date operation tips: How to easily get the day of the week for a specified date?

PHPz
PHPzOriginal
2024-03-19 21:03:031060browse

PHP date operation tips: How to easily get the day of the week for a specified date?

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:

  1. 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]}";
  2. 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}";
  3. 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!

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