<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// This article is not about the code example, just a preliminary content example</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"PHP Time Handling Notes"</span></span><span>;
</span><span><span class="hljs-meta">?></span></span><span>
<p><hr></p>
<p></span># How to Use strftime() and date_default_timezone_set() to Set Timezone and Display Local Time Correctly<span></p>
<p>When developing in PHP, handling time is a very common requirement. If we need to display the local time of users in different locations, we must set the timezone properly; otherwise, the displayed time may be incorrect. This article explains how to combine <code></span><span><span class="hljs-title function_ invoke__">strftime</span></span><span>()
After setting this, all PHP time functions will use Shanghai timezone as reference. Without this, PHP might default to UTC or the server system timezone, causing time differences.
strftime() formats date and time in a localized manner. It relies on the current locale and timezone. Compared to date(), strftime() is more suitable for scenarios requiring localized output.
Here’s a simple example:
<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">"%Y-%m-%d %H:%M:%S"</span></span><span>);
</span></span>
If the timezone is not set, the output here may not match the expected local time.
By combining both, you can ensure the output time reflects the correct local time for the specified timezone:
<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// Set timezone to Shanghai</span></span><span>
</span><span><span class="hljs-title function_ invoke__">date_default_timezone_set</span></span><span>(</span><span><span class="hljs-string">'Asia/Shanghai'</span></span><span>);
</span><span><span class="hljs-comment">// Format and display current time</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">"%Y-%m-%d %H:%M:%S"</span></span><span>);
</span><span><span class="hljs-meta">?></span></span><span>
</span></span>
This output shows the current time in Shanghai timezone.
Multi-user systems: If users are located in different countries or regions, you can save their timezone information in their settings and dynamically call date_default_timezone_set() when they access the system.
Logging: Use UTC consistently in log files, and convert to local time for display to ensure consistent records and user-friendly presentation.
International websites: Combining setlocale() with strftime() ensures both correct timezone handling and localized date display according to the user’s language preferences.
Using date_default_timezone_set() ensures that all PHP internal time functions operate based on the correct timezone. Meanwhile, strftime() helps format time in a localized way. Together, they guarantee that local time is displayed accurately in different environments, avoiding errors and confusion.
<span></span>