Home > Database > Mysql Tutorial > How to convert date to number in mysql

How to convert date to number in mysql

青灯夜游
Release: 2022-06-21 17:16:14
Original
5617 people have browsed it

2 ways to convert dates into numbers: 1. Use the TO_DAYS() function to convert the specified date value into an integer value representing the number of days, the syntax is "TO_DAYS (specified date value)". 2. Use the TIME_TO_SEC() function to convert the specified date and time value into an integer value representing the number of seconds. The syntax is "TIME_TO_SEC (specified date and time value)".

How to convert date to number in mysql

The operating environment of this tutorial: windows7 system, mysql8 version, Dell G3 computer.

Mysql 2 ways to convert dates into numbers

##1. Use the TO_DAYS() function

TO_DAYS() function returns the number of days between date and year 0 (date "0000-00-00").

That is: given a date, return a number of days (the number of days since the year is 0).

Example:


select to_days('2022-01-08');
Copy after login

How to convert date to number in mysql

2. Use the TIME_TO_SEC() function

TIME_TO_SEC (time) The function returns the time value converted from the parameter time into seconds. The conversion formula is "hour × 3600 minutes × 60 seconds".

SELECT TIME_TO_SEC('2022-01-08 15:15:15');
Copy after login

How to convert date to number in mysql

It can be seen from the execution results that according to the calculation formula "15×3600 15×60 25", the result number of seconds is 54915.

[Related recommendations:

mysql video tutorial]

The above is the detailed content of How to convert date to number in mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template