Current Location: Home> Latest Articles> How to Use the strftime Function to Get the Numeric Representation of the Day of the Week? Easily Handle Date Conversion

How to Use the strftime Function to Get the Numeric Representation of the Day of the Week? Easily Handle Date Conversion

gitbox 2025-09-03
<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// This part is unrelated to the article content, it can be any PHP code</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Welcome to the PHP Date Tool!\n"</span></span><span>;
</span><span><span class="hljs-variable">$today</span></span><span> = </span><span><span class="hljs-title function_ invoke__">date</span></span><span>(</span><span><span class="hljs-string">"Y-m-d"</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Today's date is: <span class="hljs-subst">$today</span></span></span><span>\n";
</span><span><span class="hljs-meta">?&gt;</span></span><span>
<p><hr></p>
<p></span><?php<br>
/**</p>
<ul>
<li>
<p>Article Body: How to Use the strftime Function to Get the Numeric Representation of the Day of the Week? Easily Handle Date Conversion</p>
</li>
<li></li>
<li>
<p>In PHP, sometimes we need to convert a date into its numeric representation of the day of the week (commonly 0 for Sunday, 1 for Monday, and so on).</p>
</li>
<li>
<p>For this need, PHP provides a very handy function — strftime().</p>
</li>
<li></li>
<li>
<ol>
<li>
<p>Introduction to strftime</p>
</li>
</ol>
</li>
<li>
<p>The strftime() function returns formatted date and time information based on a given format string. Its basic syntax is as follows:</p>
</li>
<li></li>
<li>
<p>string strftime(string $format [, int $timestamp = time()])</p>
</li>
<li></li>
<li>
<ul>
<li>
<p>$format: The format string, for example "%w" represents the numeric value of the day of the week (0-6, 0=Sunday).</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>$timestamp: An optional timestamp. If not specified, the current time is used.</p>
</li>
</ul>
</li>
<li></li>
<li>
<ol start="2">
<li>
<p>Getting the Numeric Representation of the Day of the Week</p>
</li>
</ol>
</li>
<li>
<p>Below is an example of converting a specific date into the numeric value of the day of the week:<br>
*/</p>
</li>
</ul>
<p>// Set the timezone to avoid timezone offsets<br>
date_default_timezone_set('Asia/Shanghai');</p>
<p>// Suppose we have a date string<br>
$dateStr = "2025-08-28";</p>
<p>// Convert the date string into a timestamp<br>
$timestamp = strtotime($dateStr);</p>
<p>// Use strftime to get the numeric value of the day of the week<br>
$weekNumber = strftime("%w", $timestamp);</p>
<p>echo "The date $dateStr corresponds to weekday number: $weekNumber\n"; // Output: 4, which means Thursday</p>
<p>/**</p>
<ul>
<li>
<p>3. Key Points</p>
</li>
<li>
<ol>
<li>
<p>%w Format Symbol:</p>
</li>
</ol>
</li>
<li>
<ul>
<li>
<p>0 = Sunday</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>1 = Monday</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>2 = Tuesday</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>3 = Wednesday</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>4 = Thursday</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>5 = Friday</p>
</li>
</ul>
</li>
<li>
<ul>
<li>
<p>6 = Saturday</p>
</li>
</ul>
</li>
<li></li>
<li>
<ol start="2">
<li>
<p>Timestamp Conversion:</p>
</li>
</ol>
</li>
<li>
<ul>
<li>
<p>Using strtotime() makes it easy to convert a date string into a timestamp, ensuring compatibility with the strftime function.</p>
</li>
</ul>
</li>
<li></li>
<li>
<ol start="4">
<li>
<p>Complete Function Example</p>
</li>
</ol>
</li>
<li>
<p>If you want to reuse it often, you can wrap it into a function:<br>
*/</p>
</li>
</ul>
<p>function getWeekNumber($date) {<br>
$timestamp = strtotime($date);<br>
return strftime("%w", $timestamp);<br>
}</p>
<p>// Test the function<br>
echo "Using the wrapped function: The date $dateStr corresponds to weekday number: " . getWeekNumber($dateStr) . "\n";</p>
<p>/**</p>
<ul data-is-only-node="" data-is-last-node="">
<li>
<p>5. Summary</p>
</li>
<li>
<p>By using strftime("%w", $timestamp), you can easily get the numeric value of the day of the week for a given date.</p>
</li>
<li data-is-last-node="">
<p data-is-last-node="">Combined with strtotime(), you can handle any date string format and quickly convert dates.<br>
*/<br>
?><br>
</span>