59.4k times

I use a dynamic calendar on my website that populates the date result into a textbox after a user makes a selection. The user will then click a SUBMIT button and POST this result to a PHP script on the server. Once this date string is POSTed to the server, how can I figure out the day of the week that this date occurs?

For example, if a user selects February 14, 2022 from the dynamic calendar, the textbox will be populated with a date string such as:


The PHP script should be able to take this value, and figure out that the day of the week in this case is Monday.

Using PHP, how can I take this date string and output the day of the week?

add a comment

3 Answers

  • Votes
  • Oldest
  • Latest

The following line will convert a date string (format can vary) and output the day of the week that it occurs:

echo date('l', strtotime($date));

To provide a more specific example of using this, the following will display Sunday:

$date = '2021/10/31'; 
$weekday = date('l', strtotime($date)); // note: first argument to date() is lower-case L
echo $weekday; // SHOULD display Sunday

The script is not dependent on the input structure of the date, providing it is valid, so you can customize it to your needs. That means any of the following dates as input would work:

November 25, 2021
Dec 31, 2021

and much more. You can see that the strtotime function in PHP is very good at figuring out the intended date.

add a comment

If you already have a timestamp stored in a variable, then you don't have to use the strtotime function as mentioned in the other solution. You can simply do the following:

echo date('l', $date));

That will take a date timestamp and output the actual day of the week that the date occurred. This is more efficient if you don't need to perform the extra step to convert a date string first.

add a comment
184 4

The following solution uses the DateTime class that PHP provides. Just like the strtotime function, this class is also good about taking a number of different supported date formats and figuring out the intended date. The following example will take a provided date, add 1 year 12 days to that date, and then output the day of the week in a full textual representation of that day, as well as an abbreviated version of that day using 3 letters:


// Create a new instance
$today = new DateTime('2015-11-05');

// Look a year into the future for example sake
$today->modify('+1 year 12 days');

// Display full day name
echo $today->format('l') . PHP_EOL; // lowercase L

// Display three-letter day name
echo $today->format('D') . PHP_EOL;


Here is the resulting output of running the PHP script:

$ php -f _.php 
add a comment