Get milliseconds of a given date in MySQL


Is there a MySQL function that gives me the milliseconds of a date or how should I do one? I've been looking for some time if I had a function like JavaScript that is getTime () that returns the number of milliseconds since 1970/01/01 but I think there is not.

asked by Ivan92 06.04.2018 в 00:54

2 answers


You can try the UNIX_TIMESTAMP () function of Mysql. Example:

SELECT UNIX_TIMESTAMP(campoFecha) - UNIX_TIMESTAMP("2018-04-05 00:00:00") 
FROM table
answered by 06.04.2018 / 01:35

You can use UNIX_TIMESTAMP, to get the range of differences between 2 dates

SET @fechaUno = '2018-01-01 00:00:00';
SET @fechaDos = '2009-01-01 00:00:00';


The UNIX_TIMESTAMP function, takes as the default starting value of   '1970-01-01 00:00:00', in GMT time format, which grants a   Time measurement based on atomic character clocks, consider that if you do not assign a default value to this function, it will take the current date and time.

GMT is used, or better known as greenwich mean time, which is a measure of international standard time.

For this calculation to work for you, you need the variables to contain the format YYYY-MM-DD HH: MM: SS

In the end you can help with this google tool, to transform the amount obtained from seconds to days, months, years or whatever you require


answered by 06.04.2018 в 01:51