MySQL – How to Select Different Time Formats

datetimeMySQLtime

I looked on the internet and I found your post on the stackexchange.
I will try to explain in more detail what we really need
I do not know well to write English
It is a system for airporttaxi

id   date       time        direction

7   2014-04-15  21:35:00    to
8   2014-04-15  15:20:00    from
9   2014-04-15  17:35:00    to
10  2014-04-15  08:15:00    to
11  2014-04-15  14:40:00    to
12  2014-04-15  11:35:00    to
13  2014-04-15  14:35:00    from

from / to = from airport / to airport

I need a mysql script we calculate the difference between the time of driving the same day
example

script needs to sort the first time

08:15:00
11:35:00
14:35:00
14:40:00
15:20:00
17:35:00
21:35:00

then calculate the difference between each ride, starting with the first

I want something like this

start        end          dif_min

08:15:00     11:35:00     200
11:35:00     14:35:00     180
14:35:00     14:40:00     5
14:40:00     15:20:00     40
15:20:00     17:35:00     135
17:35:00     21:35:00     240
21:35:00     00:00:00     25

I much appreciate it if you help me, and I hope you understand what I want

Best Answer

THIS IS THE QUERY YOU NEED

SET @GivenDate = '2014-04-15';
SELECT CONCAT(@GivenDate,' ',MAX(time)) INTO @LastDateTime
FROM airport_taxi WHERE date=@GivenDate;
SET @nexttime = 0;
SELECT
    TIME(FROM_UNIXTIME(StartTime)) "start",
    TIME(FROM_UNIXTIME(EndTime)) "end",
    FLOOR((EndTime - StartTime)/60) diff_min
FROM (SELECT (@prevtime:=@nexttime) StartTime,(@nexttime:=dttm) EndTime
FROM (SELECT FLOOR(UNIX_TIMESTAMP(CONCAT(date,' ',time))) dttm
FROM airport_taxi WHERE date=@GivenDate ORDER BY time) AA) A WHERE StartTime>0
UNION SELECT
    TIME(@LastDateTime),TIME(DATE(@LastDateTime+INTERVAL 1 DAY) + INTERVAL 0 SECOND),
    FLOOR((UNIX_TIMESTAMP(DATE(@LastDateTime+INTERVAL 1 DAY) + INTERVAL 0 SECOND) -
    UNIX_TIMESTAMP(@LastDateTime))/60) 
;

SAMPLE DATA

mysql> use test
Database changed
mysql> drop table if exists airport_taxi;
Query OK, 0 rows affected (0.13 sec)

mysql> create table airport_taxi
    -> (id int not null auto_increment primary key,
    -> date date,time time,direction varchar(10));
Query OK, 0 rows affected (0.38 sec)

mysql> insert into airport_taxi values
    -> ( 7,'2014-04-15','21:35:00','to'),
    -> ( 8,'2014-04-15','15:20:00','from'),
    -> ( 9,'2014-04-15','17:35:00','to'),
    -> (10,'2014-04-15','08:15:00','to'),
    -> (11,'2014-04-15','14:40:00','to'),
    -> (12,'2014-04-15','11:35:00','to'),
    -> (13,'2014-04-15','14:35:00','from');
Query OK, 7 rows affected (0.21 sec)
Records: 7  Duplicates: 0  Warnings: 0

mysql> select * from airport_taxi;
+----+------------+----------+-----------+
| id | date       | time     | direction |
+----+------------+----------+-----------+
|  7 | 2014-04-15 | 21:35:00 | to        |
|  8 | 2014-04-15 | 15:20:00 | from      |
|  9 | 2014-04-15 | 17:35:00 | to        |
| 10 | 2014-04-15 | 08:15:00 | to        |
| 11 | 2014-04-15 | 14:40:00 | to        |
| 12 | 2014-04-15 | 11:35:00 | to        |
| 13 | 2014-04-15 | 14:35:00 | from      |
+----+------------+----------+-----------+
7 rows in set (0.00 sec)

QUERY EXECUTED

mysql> SET @GivenDate = '2014-04-15';
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT CONCAT(@GivenDate,' ',MAX(time)) INTO @LastDateTime
    -> FROM airport_taxi WHERE date=@GivenDate;
Query OK, 1 row affected (0.00 sec)

mysql> SET @nexttime = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT
    ->     TIME(FROM_UNIXTIME(StartTime)) "start",
    ->     TIME(FROM_UNIXTIME(EndTime)) "end",
    ->     FLOOR((EndTime - StartTime)/60) diff_min
    -> FROM (SELECT (@prevtime:=@nexttime) StartTime,(@nexttime:=dttm) EndTime
    -> FROM (SELECT FLOOR(UNIX_TIMESTAMP(CONCAT(date,' ',time))) dttm
    -> FROM airport_taxi WHERE date=@GivenDate ORDER BY time) AA) A WHERE StartTime>0
    -> UNION SELECT
    ->     TIME(@LastDateTime),TIME(DATE(@LastDateTime+INTERVAL 1 DAY) + INTERVAL 0 SECOND),
    ->     FLOOR((UNIX_TIMESTAMP(DATE(@LastDateTime+INTERVAL 1 DAY) + INTERVAL 0 SECOND) -
    ->     UNIX_TIMESTAMP(@LastDateTime))/60)
    -> ;
+----------+----------+----------+
| start    | end      | diff_min |
+----------+----------+----------+
| 08:15:00 | 11:35:00 |      200 |
| 11:35:00 | 14:35:00 |      180 |
| 14:35:00 | 14:40:00 |        5 |
| 14:40:00 | 15:20:00 |       40 |
| 15:20:00 | 17:35:00 |      135 |
| 17:35:00 | 21:35:00 |      240 |
| 21:35:00 | 00:00:00 |      145 |
+----------+----------+----------+
7 rows in set (0.00 sec)

mysql>

Just remember to set the @GivenDate to whatever date you need

BTW Your last line is based on midnight the next day. Therefore, the diff_min is 145 not 25. If you have a knock-off time of 10:00 PM at th eairport, then change

UNION SELECT
    TIME(@LastDateTime),TIME(DATE(@LastDateTime + INTERVAL 1 DAY) + INTERVAL 0 SECOND),
    FLOOR((UNIX_TIMESTAMP(DATE(@LastDateTime + INTERVAL 1 DAY) + INTERVAL 0 SECOND) -
    UNIX_TIMESTAMP(@LastDateTime))/60) 

to this

UNION SELECT
    TIME(@LastDateTime),TIME(DATE(@LastDateTime + INTERVAL 1 DAY) + INTERVAL 0 SECOND),
    FLOOR((UNIX_TIMESTAMP(DATE(@LastDateTime + INTERVAL 1 DAY) + INTERVAL 0 SECOND) -
    UNIX_TIMESTAMP(@LastDateTime))/60) - 120

Subtracting 120 backs 2 hours away from midnight of the next day makes the last line's end 10:00 PM

Give it a Try !!!

UPDATE 2014-04-16 09:44 EDT

I cannot really test this, but you can try this:

SET @GivenDate = '2014-04-15';
SELECT CONCAT(@GivenDate,' ',MAX(time)) INTO @LastDateTime
FROM airport_taxi WHERE date=@GivenDate;
SELECT id INTO @LastID FROM airport_taxi
WHERE date=@GivenDate ORDER BY time DESC LIMIT 1;
SET @nexttime = 0;
SELECT
    id,
    TIME(FROM_UNIXTIME(StartTime)) "start",
    TIME(FROM_UNIXTIME(EndTime)) "end",
    FLOOR((EndTime - StartTime)/60) diff_min
FROM (SELECT id,(@prevtime:=@nexttime) StartTime,(@nexttime:=dttm) EndTime
FROM (SELECT id,FLOOR(UNIX_TIMESTAMP(CONCAT(date,' ',time))) dttm
FROM airport_taxi WHERE date=@GivenDate ORDER BY time) AA) A WHERE StartTime>0
UNION SELECT @LastID,
    TIME(@LastDateTime),TIME(DATE(@LastDateTime+INTERVAL 1 DAY) + INTERVAL 0 SECOND),
    FLOOR((UNIX_TIMESTAMP(DATE(@LastDateTime+INTERVAL 1 DAY) + INTERVAL 0 SECOND) -
    UNIX_TIMESTAMP(@LastDateTime))/60) 
;

I hope this helps !!!