You can get the full-day name (e.g., Monday, Tuesday, etc.) from a date in PHP using the DateTime class. The DateTime class provides a method called format() that allows you to format dates and extract specific components like the full-day name. Here’s a step-by-step guide on how to achieve this:
Step 1: Get the date from which you want to extract the full-day name. This can be from user input, a database, or any other data source.
$date_string = '2023-05-25'; // Replace this with your date string
Step 2: Create a DateTime
the object for the given date string.
$date = new DateTime($date_string);
Step 3: Use the format()
method to get the full-day name from the DateTime
object.
$full_day_name = $date->format('l');
Here’s the complete code in one block:
<?php
$date_string = '2023-05-25'; // Replace this with your date string
$date = new DateTime($date_string);
$full_day_name = $date->format('l');
echo "Full Day Name: " . $full_day_name;
?>
For the date string '2023-05-25'
, the output will be:
Full Day Name: Wednesday
The format 'l'
in the format()
the method represents the full textual representation of the day of the week (e.g., “Sunday”, “Monday”, “Tuesday”, etc.). If you want to get the abbreviated day name (e.g., “Sun”, “Mon”, “Tue”), you can use 'D'
as the format string.
You can use various format options with the format()
method to extract different components of the date, such as the month, year, time, etc. For more information on date formats, you can refer to the PHP documentation for the date()
function, as the format()
method uses the same format codes.