MySQL DateTime instance can be converted to seconds via UNIX_TIMESTAMP() function as follows -
mysql> Select UNIX_TIMESTAMP('2017-05-15 04:05:30') AS 'NUMBER OF SECONDS'; +-------------------+ | NUMBER OF SECONDS | +-------------------+ | 1494801330 | +-------------------+ 1 row in set (0.00 sec)
The above query will convert the given datetime instance to total seconds .
mysql> Select UNIX_TIMESTAMP(NOW()) AS 'NUMBER OF SECONDS'; +-------------------+ | NUMBER OF SECONDS | +-------------------+ | 1509248856 | +-------------------+ 1 row in set (0.00 sec)
The above query will convert the current DateTime instance into total seconds.
mysql> Select UNIX_TIMESTAMP(Dateofreg) AS 'NUMBER OF SECONDS' from testing where StudentName = 'Gaurav'; +-------------------+ | NUMBER OF SECONDS | +-------------------+ | 1509247113 | +-------------------+ 1 row in set (0.00 sec)
The above query will convert a DateTime instance which is a stored value in table "testing" where column StudentName has the value "Gaurav".
The above is the detailed content of How to get total seconds from MySQL DATETIME instance?. For more information, please follow other related articles on the PHP Chinese website!