Home > Article > Backend Development > How to convert time type to text format in php (three methods)
With the development of Web technology, more and more Web applications involve time type processing, and PHP, as a server-side programming language, is no exception. In PHP, in some cases we need to convert the time type into a text type for better display or storage. This article will introduce you to several methods of converting time types into text in PHP.
Method 1: date() function
The most commonly used method of converting time format into text in PHP is to use the date() function. This function formats a Unix timestamp into a human-readable date and time.
Sample code:
$date = date('Y-m-d H:i:s', time()); echo $date;
Analysis:
In the above code, we call the date() function, where the first parameter specifies the format of the formatted date string. , the second parameter is the Unix timestamp, which is the current timestamp. Here we format the date string into year-month-day hour:minute:second format and output it to the screen.
Method 2: strftime() function
In addition to the date() function, PHP also provides another method for formatting date and time strings, namely the strftime() function. This function has two parameters, the first parameter specifies the format of the formatted date string, and the second parameter is the Unix timestamp.
Sample code:
setlocale(LC_TIME, 'zh_CN.UTF-8'); // 设置区域 $date = strftime('%Y-%m-%d %H:%M:%S', time()); echo $date;
Analysis:
In the above code, we first call the setlocale() function to set the localization information, and the first parameter specifies the localization Environment, we set it to Simplified Chinese here. Then we called the strftime() function, where the first parameter formatted the date string into year-month-day hour:minute:second format and output it to the screen.
Method 3: DateTime class
In addition to using the date() and strftime() functions to convert time to text, PHP also provides a DateTime class to handle time-related operations. This class provides a series of methods to control the output time format in more detail.
Sample code:
$date = new DateTime(); echo $date->format('Y-m-d H:i:s');
Analysis:
In the above code, we use the DateTime class in PHP to represent the current time, and use the format() method to format it Convert the date string into year-month-day hour:minute:second format and output it to the screen.
When using the DateTime class, we can use the methods provided by this class to perform more operations, such as:
$date = new DateTime(); $date->add(new DateInterval('P1D')); // 增加一天 echo $date->format('Y-m-d H:i:s');
Analysis:
In the above code, we call Use the add() method of the DateTime class to add the time of day, and then use the format() method again to output the formatted date string.
Conclusion
Converting time to text is one of the more basic operations in PHP. This article introduces three methods of converting time to text in PHP, namely using the date() function and strftime. () function and the DateTime class. Different methods are suitable for different scenarios, and choosing the appropriate method can better meet business needs. When you need to use these methods, just pay attention to the parameter transfer and format setting.
The above is the detailed content of How to convert time type to text format in php (three methods). For more information, please follow other related articles on the PHP Chinese website!