0
0
PhpHow-ToBeginner · 3 min read

How to Get Day of Week in PHP: Simple Examples

In PHP, you can get the day of the week using the date() function with the format character 'l' for full day name or 'D' for short day name. For example, date('l') returns the full name of the current day like "Monday".
📐

Syntax

The basic syntax to get the day of the week in PHP is using the date() function with a format string.

  • date('l'): Returns the full name of the day (e.g., Monday).
  • date('D'): Returns the short name of the day (e.g., Mon).
  • date('w'): Returns the numeric representation of the day (0 for Sunday through 6 for Saturday).
php
date('l'); // Full day name

date('D'); // Short day name

date('w'); // Numeric day of week
💻

Example

This example shows how to get and print the full day name of the current date and a specific date.

php
<?php
// Get full day name of today
$today = date('l');
echo "Today is: $today\n";

// Get day of week for a specific date
$date = '2024-06-15';
$timestamp = strtotime($date);
$dayOfWeek = date('l', $timestamp);
echo "The day of week for $date is: $dayOfWeek\n";
?>
Output
Today is: Saturday The day of week for 2024-06-15 is: Saturday
⚠️

Common Pitfalls

Common mistakes include:

  • Not converting a date string to a timestamp before using date(), which causes incorrect results.
  • Using wrong format characters, like 'd' which returns day of the month, not day of the week.
  • Forgetting that date() uses the current server timezone unless changed.
php
<?php
// Wrong: Using date() directly on a date string
$date = '2024-06-15';
echo date('l', $date); // Incorrect, $date is not a timestamp

// Right: Convert string to timestamp first
$timestamp = strtotime($date);
echo date('l', $timestamp); // Correct
?>
Output
Warning: date() expects parameter 2 to be int, string given in ... Saturday
📊

Quick Reference

Format CharacterMeaningExample Output
l (lowercase L)Full day nameMonday
DShort day nameMon
wNumeric day of week (0=Sunday)0
NISO-8601 numeric day (1=Monday)1

Key Takeaways

Use date('l') to get the full day name of the current date in PHP.
Convert date strings to timestamps with strtotime() before using date() for specific dates.
Remember date() format characters differ; 'd' is day of month, 'l' is day of week.
Server timezone affects date() output unless explicitly set.
Use numeric formats like 'w' or 'N' for day of week numbers.