Current Location: Home> Latest Articles> PHP Guide to Getting the Day of the Week: Using date() and DateTime Easily

PHP Guide to Getting the Day of the Week: Using date() and DateTime Easily

M66 2025-09-16

PHP Date Programming Guide: Easily Determine the Day of the Week

In PHP development, handling dates and times is a common requirement, and one typical task is to find out the day of the week for a specific date. PHP provides rich date and time functions, making this task simple. This article will explain how to get the day of the week for a given date in PHP, with ready-to-use example code.

Using date() Function to Get the Day of the Week

The PHP date() function formats date and time, including retrieving the day of the week. By specifying the "l" parameter, you can get the day of the week for a given date. Example code:

$date = "2022-01-15";
$day_of_week = date('l', strtotime($date));
echo "The date $date is $day_of_week";

Using strtotime() to Convert Date Formats

In the above example, we used strtotime() to convert a date string into a Unix timestamp so that the date() function can correctly interpret it. strtotime() supports various date formats, such as "2022-01-15" or "January 15, 2022", making it flexible to handle different date inputs.

Using DateTime Class to Get the Day of the Week

Besides the date() function, PHP offers the DateTime class for handling dates and times. DateTime provides more flexible date operations. Example code:

$date = new DateTime("2022-01-15");
$day_of_week = $date->format('l');
echo "The date 2022-01-15 is $day_of_week";

Important Notes

  • Ensure the input date format is correct. You can use strtotime() to validate the date string.
  • Consider timezone effects, especially when handling dates across different timezones.
  • Familiarize yourself with different date formats to handle various scenarios efficiently.

Conclusion

After reading this guide, you now know how to determine the day of the week for a specific date in PHP. Whether using the date() function or the DateTime class, you can easily achieve this. Choosing the right method based on your needs will improve development efficiency. We hope this guide helps you in your PHP programming journey. Happy coding!