To get the day from a date in PHP, you can use the date()
function in conjunction with strtotime()
or the DateTime
class.
1. Using date()
and strtotime()
:
The date()
function formats a local date/time, and strtotime()
parses an English textual datetime description into a Unix timestamp.
<?php
$dateString = '2025-10-07'; // Your date string
$dayNameFull = date('l', strtotime($dateString)); // Full day name (e.g., Tuesday)
$dayNameShort = date('D', strtotime($dateString)); // Short day name (e.g., Tue)
$dayOfMonth = date('d', strtotime($dateString)); // Day of the month (e.g., 07)
echo "Full day name: " . $dayNameFull . "\n";
echo "Short day name: " . $dayNameShort . "\n";
echo "Day of the month: " . $dayOfMonth . "\n";
?>
2. Using the DateTime
class:
The DateTime
class provides a more object-oriented approach to working with dates and times.
<?php
$dateString = '2025-10-07'; // Your date string
$dateTime = new DateTime($dateString);
$dayNameFull = $dateTime->format('l'); // Full day name (e.g., Tuesday)
$dayNameShort = $dateTime->format('D'); // Short day name (e.g., Tue)
$dayOfMonth = $dateTime->format('d'); // Day of the month (e.g., 07)
echo "Full day name: " . $dayNameFull . "\n";
echo "Short day name: " . $dayNameShort . "\n";
echo "Day of the month: " . $dayOfMonth . "\n";
?>
Explanation of Format Characters:
l
(lowercase L): A full textual representation of the day of the week (e.g., “Sunday” through “Saturday”).D
: A three-letter textual representation of a day (e.g., “Mon” through “Sun”).d
: Day of the month, 2 digits with leading zeros (e.g., “01” to “31”).