UNIX_TIMESTAMP calculation : UNIX_TIMESTAMP « Date Time « SQL / MySQL






UNIX_TIMESTAMP calculation

     
mysql>
mysql> CREATE TABLE mytable (
    ->     id INT AUTO_INCREMENT,
    ->     begintime DATETIME,
    ->     endtime DATETIME,
    ->     PRIMARY KEY (id)
    -> );
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql> INSERT INTO mytable (begintime, endtime) VALUES
    ->         ('2005-03-27 7:15', '2005-03-27 18:00'),
    ->         ('2005-03-28 8:00', '2005-03-28 18:00'),
    ->         ('2005-03-29 7:30', '2005-03-29 16:50'),
    ->         ('2005-03-30 7:00', '2005-03-30 17:15');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql>
mysql> SELECT DATE_FORMAT(begintime, '%Y-%m-%d') AS dt,
    -> UNIX_TIMESTAMP(endtime) - UNIX_TIMESTAMP(begintime) AS s
    -> FROM mytable;
+------------+-------+
| dt         | s     |
+------------+-------+
| 2005-03-27 | 38700 |
| 2005-03-28 | 36000 |
| 2005-03-29 | 33600 |
| 2005-03-30 | 36900 |
+------------+-------+
4 rows in set (0.00 sec)

mysql>
mysql>
mysql> drop table mytable;
Query OK, 0 rows affected (0.00 sec)

mysql>
mysql>

   
    
    
    
    
  








Related examples in the same category

1.UNIX_TIMESTAMP( ) can convert DATE values to seconds.
2.Converting Between Date-and-Time Values and Seconds
3.The number of seconds between dates that lie two weeks apart can be computed like this:
4.To convert the interval in seconds to other units, perform the appropriate arithmetic operation.