PHP - Get day of week from a specific date

By xngo on August 4, 2019

In PHP, there are multiple ways to get the day of the week from a specific date. Here are the examples.

Using date() & strtotime()

You can use date() & strtotime() functions to get the day of the week. However, by doing this way, you are required to use the Unix timestamp which is not future proof. It will cause problem after year 2038. It is recommended to use DateTime class. See the next section below.

<?php
 
// Set a specific date.
$timestamp = strtotime('2019-08-04');
 
// Get day of the week.
$dw = date( 'w', $timestamp);
echo $dw;       // Output: 0 for Sunday.
 
/* Starting from PHP v5.1.0, you can use the 'N' option.
    It will return value within 1 to 7 where Monday = 1. */
$dw_new = date( 'N', $timestamp);
echo $dw_new;   // Output: 7 for Sunday.
 
?>

Using DateTime class

<?php
 
// Set a specific date.
$dt = new DateTime();
$dt->setDate(2019, 8, 4);
 
// Get day of the week.
echo $dt->format('w');   // Output: 0 for Sunday.
 
/* Starting from PHP v5.1.0, you can use the 'N' option.
    It will return value within 1 to 7 where Monday = 1. */
echo $dt->format('N');   // Output: 7 for Sunday.
 
?>

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.