Home >Database >Mysql Tutorial >How to Convert MySQL Date Strings to UNIX Timestamps and Vice Versa?
To convert a date string in the format "Apr 15 2012 12:00AM" to a UNIX timestamp, use the following SQL query:
SELECT UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p'))
To modify the date format, use the FROM_UNIXTIME() function along with the UNIX_TIMESTAMP() function. For example, the following query converts the UNIX timestamp to the format "%m-%d-%Y %h:%i:%p":
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(STR_TO_DATE('Apr 15 2012 12:00AM', '%M %d %Y %h:%i%p')), '%m-%d-%Y %h:%i:%p')
For more information, refer to the following MySQL documentation:
The above is the detailed content of How to Convert MySQL Date Strings to UNIX Timestamps and Vice Versa?. For more information, please follow other related articles on the PHP Chinese website!