Use SEC_TO_TIME to make time more understandable : SEC_TO_TIME « Date Time « SQL / MySQL






Use SEC_TO_TIME to make time more understandable

    
mysql>
mysql>
mysql> CREATE TABLE mytable (
    ->     id INT AUTO_INCREMENT,
    ->     begintime DATETIME,
    ->     endtime DATETIME,
    ->     PRIMARY KEY (id)
    -> );
Query OK, 0 rows affected (0.01 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>
mysql> SELECT DATE_FORMAT(begintime, '%Y-%m-%d') AS dt,
    -> SEC_TO_TIME(UNIX_TIMESTAMP(endtime) - UNIX_TIMESTAMP(begintime)) AS t
    -> FROM mytable;
+------------+----------+
| dt         | t        |
+------------+----------+
| 2005-03-27 | 10:45:00 |
| 2005-03-28 | 10:00:00 |
| 2005-03-29 | 09:20:00 |
| 2005-03-30 | 10:15:00 |
+------------+----------+
4 rows in set (0.00 sec)

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

   
    
    
    
  








Related examples in the same category

1.SEC_TO_TIME(MOD(TIME_TO_SEC(t1) + TIME_TO_SEC(t2), 86400))
2.SEC_TO_TIME( ) converts the equivalent number of seconds to a TIME value
3.To convert the interval to a TIME value, pass it to SEC_TO_TIME( ):