Home > Database > Mysql Tutorial > How to Convert MySQL Date Strings to UNIX Timestamps and Vice Versa?

How to Convert MySQL Date Strings to UNIX Timestamps and Vice Versa?

Linda Hamilton
Release: 2024-11-29 22:28:12
Original
666 people have browsed it

How to Convert MySQL Date Strings to UNIX Timestamps and Vice Versa?

Converting MySQL Date Strings to UNIX Timestamps

Convert Date String to UNIX Timestamp

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'))
Copy after login

Modify Date Format

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')
Copy after login

Documentation

For more information, refer to the following MySQL documentation:

  • [UNIX_TIMESTAMP](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_unix-timestamp)
  • [FROM_UNIXTIME](https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_from-unixtime)

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template