In PHP, the strftime() function is used to format dates and times. It takes two parameters: a format string and an optional timestamp. Based on the provided format, strftime() returns a formatted date and time string. The format string includes special conversion specifiers that start with the percentage symbol (%).
Conversion specifiers are used to define how the date and time should be formatted. Some common conversion specifiers are:
For more conversion specifiers, you can refer to the official PHP documentation.
Here is an example of how to use the strftime() function to get the current time:
The above code will output the current time in the format "Year-Month-Day Hour:Minute:Second".
If you want to get a localized version of the date and time, you can use strftime() in combination with the setlocale() function. For example, the following code will output the time in a Chinese format:
This code will output the localized date and time, such as "2025年06月19日 星期四 14:30:45".
When using strftime(), you might encounter incorrect date formatting due to an improper system locale setting. You can adjust the locale using the setlocale() function. For example, to set the locale to American English, use the following code:
Note that the behavior of strftime() may differ across operating systems, as it depends on the system's C library.
The strftime() function is a powerful tool in PHP for formatting dates and times. Whether you need to output standard formats or generate localized dates and times, strftime() offers robust support. Be sure to properly set the locale to avoid formatting issues.