Mysql time conversion
The conversion is widely used between time, string, and timestamp. Although it is widely used, almost every time I use it, I like to check for usage. This article will serve as a notice to sort the conversion between the three uses for easy future viewing (i.e.: date Convert string, date to timestamp, string to date, string to timestamp, timestamp to date, timestamp to string);
Common Functions for time
date_format (date, format) function, MySQL date formatting function date_format()
unix_timestamp () function
str_to_date (str, format) function
from_unixtime (unix_timestamp, format) function, MySQL timestamp formatting function from_unixtime
Time to string.
Choose the format date(now(),’ percent Y-percent m-percent d’);
#Result: 05–01–2020
Time to timestamp
Select unix timestamp(now());;;
#Result: 1452001082Result:
String to Time
Choose str to date(‘2020–01–02 ‘,’ percent Y- percent m- percent d percent H ‘);
#Outcome: 2020–01–02 00:00:00
String to Timestamp
Unix timestamp(‘2020–01–02 ‘) is selected;
#Outcome: 1451664000
Timestamp to time
Select Unixtime(1451997924) from;
#Outcome: 2020–01–05 20:45:24
Timestamp to string
Choose from ‘unixtime(1451997924,’ percent Y- percent d ‘);
/Outcome: 2020–01–05 20:45:24
I like to learn new and better ways of doing things when working on a scale, and feel free to ask questions and make suggestions.
Also, check out another story on this.
Thanks for reading this.