MySql: Calculate the Difference Between Two Timestamps

Last updated 2 years ago

...

You possess a pair of columns labeled as "timestamp" and wish to determine the duration between their values? We will check in below example.

For solving this problem i have a table flight, in which we have arrival and departure time. See the below table.

flight

idflight_nameflight_codeflight_arrivalflight_departure
1 6E24222023-05-02 05:29:512023-05-02 06:53:51
2 9I6962023-05-02 07:09:512023-05-02 08:53:51
3 AI4042023-05-02 09:25:302023-05-02 09:56:30
4 AI8122023-05-02 11:10:302023-05-02 11:56:30

 

Solution. 

SELECT id, flight_arrival, flight_departure, TIMESTAMPDIFF(SECOND, flight_arrival,flight_departure) AS standby_time FROM flight;

Output:

idflight_arrivalflight_departurestandby_time
12023-05-02 05:29:512023-05-02 06:53:515040
22023-05-02 07:09:512023-05-02 08:53:516240
32023-05-02 09:25:302023-05-02 09:56:301860
42023-05-02 11:10:302023-05-02 11:56:302760

 

To compute the disparity among the timestamps within MySQL, employ the TIMESTAMPDIFF(start, end, unit) function. The unit parameter has several options such as MICROSECOND, SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.

 


Is App Developer A Reward...

The fact that people are spending more time on their phones makes now an excellent time to start a c...

Read It

An Introduction To CSS Pr...

CSS or Cascading Style Sheets have become a really popular programming language in recent times and...

Read It



----Advertisement----