php Mysql time conversion method: 1. Use the "FROM_UNIXTIME()" method in mysql to convert UNIX timestamps to dates; 2. Use the "date()" function in php to convert UNIX timestamps is the date.
The operating environment of this article: Windows 7 system, PHP version 7.1, Dell G3 computer.
php How to convert mysql time?
How to convert PHP Mysql date and time
Programmers who have written PHP MySQL all know that there is a time difference. UNIX timestamps and formatted dates are what we often deal with. There are two time representations. Unix timestamp is convenient to store and process, but not intuitive. Formatted date is intuitive, but it is not as easy to process as Unix timestamp. Therefore, sometimes it is necessary to convert each other. Here are several ways to convert each other. Conversion method.
1. Complete in MySQL
This method is converted in the MySQL query statement. The advantage is that it does not take up the parsing time of the PHP parser and is fast. The disadvantage is that it can only be used for database queries. , there are limitations.
1. Use the function to convert UNIX timestamp to date: FROM_UNIXTIME()
General form: select FROM_UNIXTIME(1156219870);
2. Use the function to convert date to UNIX timestamp Function: UNIX_TIMESTAMP()
General form: Select UNIX_TIMESTAMP('2006-11-04 12:23:00′);
Example: mysql query the number of records for the day:
$sql=”select * from message Where DATE_FORMAT(FROM_UNIXTIME(chattime),’%Y-%m-%d’) = DATE_FORMAT(NOW(),’%Y-%m-%d’) order by id desc”;
Of course, you can also choose to convert in PHP. Let’s talk about conversion in PHP.
2. Complete in PHP
This method completes the conversion in the PHP program. The advantage is that it can be converted regardless of whether it is the data obtained by querying in the database, and the conversion range is not limited. The disadvantage is that It takes up the parsing time of the PHP parser and is relatively slow.
1. Use the function to convert UNIX timestamp to date: date()
General form: date('Y-m-d H:i:s', 1156219870);
2 . Use the function to convert date to UNIX timestamp: strtotime()
General form: strtotime('2010-03-24 08:15:42');
Recommended study: "PHP video tutorial》
The above is the detailed content of How to convert php mysql time. For more information, please follow other related articles on the PHP Chinese website!