Home  >  Article  >  Database  >  How to get total seconds from MySQL DATETIME instance?

How to get total seconds from MySQL DATETIME instance?

王林
王林forward
2023-08-27 14:29:151290browse

如何从 MySQL DATETIME 实例获取总秒数?

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete