Current Location: Home> Latest Articles> How to Use setlocale() to Set and Format Timestamps in Logs: A Practical Example

How to Use setlocale() to Set and Format Timestamps in Logs: A Practical Example

gitbox 2025-06-22

How to Use setlocale() to Set and Format Timestamps in Logs: A Practical Example

In PHP, we often need to record log information, which usually includes timestamps to mark the exact time each event occurs. To ensure the time format in log files meets project requirements or adapts to different locales, the setlocale() function and strftime() function are very important tools.

This article will explain how to use setlocale() to set and format timestamps in logs through a practical example.

1. Understanding setlocale() and strftime()

  1. setlocale() function

    The setlocale() function is used to set the program's locale. The locale affects the formatting of dates, times, numbers, currency, and more. With setlocale(), you can set locale settings to meet the needs of different regions, such as Chinese, English, French, etc.

    <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>

    The above code sets the locale to Chinese (China) and specifies UTF-8 encoding.

  2. strftime() function

    The strftime() function returns a formatted date or time string based on the given format string and the current locale. This function is very useful for custom time formatting.

    <span><span><span class="hljs-variable">$formatted_time</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-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$formatted_time</span></span><span>;  </span><span><span class="hljs-comment">// Output: 2025年06月20日 10:30:15</span></span><span>
    </span></span>

2. Using Timestamps in Logs

In real projects, we often record logs, such as system errors, user actions, task executions, etc. The timestamps in logs usually need to be formatted according to different requirements to ensure readability and consistency of the time.

Below is a practical example of how to use setlocale() and strftime() to format timestamps in log files.

3. Practical Example: Formatting Timestamps in Logs

Suppose we have a simple log recording system that requires outputting the date, time, and detailed log content in the log file. We want to format the timestamps in the logs according to the current locale.

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Set locale to Chinese (China) with UTF-8 encoding</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>);
<p></span>// Get current timestamp<br>
$current_timestamp = time();</p>
<p>// Format timestamp<br>
$formatted_time = strftime("%Y年%m月%d日 %H:%M:%S", $current_timestamp);</p>
<p>// Log content<br>
$log_message = "System started successfully!";</p>
<p>// Open log file and write log<br>
$log_file = fopen('system.log', 'a');<br>
if ($log_file) {<br>
$log_entry = $formatted_time . " - " . $log_message . "\n";<br>
fwrite($log_file, $log_entry);<br>
fclose($log_file);<br>
echo "Log recorded!";<br>
} else {<br>
</span>echo "Unable to open log file!";<br>
}<br>
</span>?><br>
</span>

Code Explanation:

  1. Setting the locale with setlocale():

    • setlocale(LC_TIME, 'zh_CN.UTF-8') sets the locale to Chinese (China) with UTF-8 encoding, so the date and time are displayed in Chinese format.

  2. Getting the current timestamp:

    • Using the time() function to get the current Unix timestamp.

  3. Formatting the timestamp:

    • strftime() formats the timestamp into a string of “Year-Month-Day Hour:Minute:Second” based on the specified format. Here, %Y is four-digit year, %m is month, %d is day, %H is hour, %M is minute, and %S is second.

  4. Writing to the log file:

    • Using fopen() to open a log file, fwrite() to write the formatted time and log content to the file, and fclose() to close the file.

  5. Log output:

    • Each log entry is written to the file in the format “YYYY年MM月DD日 HH:MM:SS - message”.

Example of Log Output:

<span><span><span class="hljs-number">2025</span></span><span><span class="hljs-string">年06月20日</span></span><span> </span><span><span class="hljs-number">10</span></span><span><span class="hljs-string">:30:15</span></span><span> </span><span><span class="hljs-bullet">-</span></span><span> </span><span><span class="hljs-string">System started successfully!</span></span><span>
</span></span>

4. Summary

By using setlocale() and strftime(), we can conveniently set and format timestamps in logs in PHP, ensuring the displayed time meets specific locale and format requirements. Time formats in logs may vary across different regions or languages, so these functions help improve the readability and localization support of logs.

In practical use, the format string can be adjusted as needed to meet specific time formatting requirements.