Current Location: Home> Latest Articles> How to Use PHP date() to Display the Day of the Week? Choose Between Numbers or Names!

How to Use PHP date() to Display the Day of the Week? Choose Between Numbers or Names!

gitbox 2025-08-26

In PHP, one of the most commonly used functions for handling dates and time is date(). When you need to display the current day of the week, date() makes it simple, and you can freely choose whether to display it as a number or as a name (in English or localized format).

Display the Day of the Week as a Number

If you want to display the day of the week in numeric form, you can use the format character w. This format will return numbers from 0 (Sunday) to 6 (Saturday).

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">date</span></span><span>(</span><span><span class="hljs-string">&#039;w&#039;</span></span><span>); </span><span><span class="hljs-comment">// Example output: 3, which means Wednesday</span></span><span>
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

This numeric form is very useful for program logic, such as deciding whether a feature should be enabled based on weekdays or weekends.

Display the Day of the Week in English

If you prefer to display the weekday name in English, you can use the format character l (lowercase letter L):

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">date</span></span><span>(</span><span><span class="hljs-string">&#039;l&#039;</span></span><span>); </span><span><span class="hljs-comment">// Example output: Wednesday</span></span><span>
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

This format returns the full English weekday name, such as Monday, Tuesday, Wednesday, and so on.

Display Abbreviated English Weekday Names

If you don’t need the full name but prefer a shorter three-letter abbreviation, you can use D:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">date</span></span><span>(</span><span><span class="hljs-string">&#039;D&#039;</span></span><span>); </span><span><span class="hljs-comment">// Example output: Wed</span></span><span>
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

This format is especially useful for headings or places where space is limited.

Use setlocale() and strftime() to Display Weekdays in Local Language (System-Dependent)

If you want to display the weekday in your local language (e.g., Chinese), you can use setlocale() together with strftime(). However, note that this may not work well on some operating systems (such as Windows).

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-title function_ invoke__">setlocale</span></span><span>(LC_TIME, </span><span><span class="hljs-string">&#039;zh_CN.UTF-8&#039;</span></span><span>); </span><span><span class="hljs-comment">// Set to Chinese</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">strftime</span></span><span>(</span><span><span class="hljs-string">&#039;%A&#039;</span></span><span>); </span><span><span class="hljs-comment">// Example output: 星期三</span></span><span>
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

Note: Starting from PHP 8.1, strftime() is deprecated. It is recommended to use the Internationalization extension (such as IntlDateFormatter) for localized date formatting.

Using the DateTime Class is Also a Great Option

Modern PHP development recommends using the DateTime class, which is more flexible and object-oriented than the traditional date() function.

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-variable">$dt</span></span><span> = </span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title class_">DateTime</span></span><span>();
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$dt</span></span><span>-&gt;</span><span><span class="hljs-title function_ invoke__">format</span></span><span>(</span><span><span class="hljs-string">&#039;l&#039;</span></span><span>); </span><span><span class="hljs-comment">// Example output: Wednesday</span></span><span>
</span><span><span class="hljs-meta">?&gt;</span></span><span>
</span></span>

You can also work with other dates by passing in custom time strings, not just the current time.

Summary

Whether it’s the numeric form of the day of the week or the English/localized weekday name, PHP’s date() function provides simple yet powerful support. If you want better maintainability and flexibility in your code, consider using the DateTime class. Mastering these techniques will make your applications smarter and more globally adaptable.