Programmers who have written PHP + MySQL know that there is a time difference. UNIX timestamps and formatting dates are two forms of time representation that we often deal with. The UNIX timestamps are easy to store and process, but not intuitive, the date format is intuitive, but it is not as easy to process as the Unix timestamp, so sometimes it needs to be converted to each other. Below are several conversion methods for mutual conversion.
I. Completed in MySQL
This method is converted into a 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 in database queries and has limitations.
1. UNIX timestamp conversion to date function: from_unixtime ()
General Format: Select from_unixtime (1156219870 );
2. convert a date to a Unix timestamp using the function: unix_timestamp ()
General Format: Select unix_timestamp ('2017-11-04 12:23:00 ′);
Example: MySQL queries the number of records on the current 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. The following describes the conversion in PHP.
2. Complete in PHP
This method completes the conversion in the PHP program. The advantage is that no matter whether the data obtained by queries in the database can be converted, the conversion range is unrestricted, and the disadvantage is that it takes up the parsing time of the PHP parser, relatively slow.
1. Convert UNIX timestamp to date using the function: Date ()
General Format: Date ('Y-m-d h: I: s', 1156219870 );
2. convert a date to a Unix timestamp using the function strtotime ()
General Format: strtotime ('2017-03-24 08:15:42 ')