Computer >> Computer tutorials >  >> Programming >> MySQL

How can I convert 1st January of the current year into epoch?


It can be done by using UNIX_TIMESTAMP() function as follows −

mysql> Select UNIX_TIMESTAMP(CONCAT(YEAR(CURDATE()),'-01-01'));
+--------------------------------------------------+
| UNIX_TIMESTAMP(CONCAT(YEAR(CURDATE()),'-01-01')) |
+--------------------------------------------------+
| 1483209000                                       |
+--------------------------------------------------+
1 row in set (0.03 sec)

It can be verified by the following query −

mysql> Select FROM_UNIXTIME(1483209000);
+---------------------------+
| FROM_UNIXTIME(1483209000) |
+---------------------------+
| 2017-01-01 00:00:00       |
+---------------------------+
1 row in set (0.02 sec)