Convert date to day name e.g. Mon, Tue, Wed
In PHP, you can use the date
function to convert a date to a day name.
Here is an example of how to do this:
<?php
function get_day_name($date)
{
// Convert the date to a timestamp
$timestamp = strtotime($date);
// Return the day name
return date('D', $timestamp);
}
// Test the function
echo get_day_name('2022-01-10'); // Output: Mon
Watch a video course
Learn object oriented PHP
The date
function takes two arguments: a format string and a timestamp. The format string 'D' represents the day name in short form (e.g., Mon, Tue, Wed). The timestamp is a numerical representation of the date, which can be obtained using the strtotime
function.
You can also use the DateTime
class to convert a date to a day name:
<?php
function get_day_name($date)
{
// Create a DateTime object
$date_obj = new DateTime($date);
// Return the day name
return $date_obj->format('D');
}
// Test the function
echo get_day_name('2022-01-10'); // Output: Mon