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).
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"><?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">'w'</span></span><span>); </span><span><span class="hljs-comment">// Example output: 3, which means Wednesday</span></span><span>
</span><span><span class="hljs-meta">?></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.
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"><?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">'l'</span></span><span>); </span><span><span class="hljs-comment">// Example output: Wednesday</span></span><span>
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
This format returns the full English weekday name, such as Monday, Tuesday, Wednesday, and so on.
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"><?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">'D'</span></span><span>); </span><span><span class="hljs-comment">// Example output: Wed</span></span><span>
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
This format is especially useful for headings or places where space is limited.
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"><?php</span></span><span>
</span><span><span class="hljs-title function_ invoke__">setlocale</span></span><span>(LC_TIME, </span><span><span class="hljs-string">'zh_CN.UTF-8'</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">'%A'</span></span><span>); </span><span><span class="hljs-comment">// Example output: 星期三</span></span><span>
</span><span><span class="hljs-meta">?></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.
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"><?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>-></span><span><span class="hljs-title function_ invoke__">format</span></span><span>(</span><span><span class="hljs-string">'l'</span></span><span>); </span><span><span class="hljs-comment">// Example output: Wednesday</span></span><span>
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
You can also work with other dates by passing in custom time strings, not just the current time.
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.