MySQL : How to calculate Elapse time between 2 date time values

MySQL : How to calculate Elapse time between 2 date time values

หากเราต้องการหาว่าระยะเวลาระหว่าง 2 ช่วงเวลาห่างกันเท่าไร ทำได้ดังนี้

ตัวอย่าง

START_TIME END_TIME
4/18/2011 7:45:38 4/18/2011 7:51:50

เราสามารถเรียกใช้ SQL ดังนี้

select start_time,end_time,
(unix_timestamp(end_time) – unix_timestamp(start_time)) sec
(unix_timestamp(end_time) – unix_timestamp(start_time))/60 min
sec_to_time(unix_timestamp(end_time) – unix_timestamp(start_time)) diff_time
from test

ได้ผลลัพธ์ดังนี้

start_time end_time sec min diff_time
2011-04-18 07:45:38 2011-04-18 07:51:50 372 6.2000 00:06:12

เท่านี้เราก็ได้ระยะเวลาที่ห่างกันแล้วครับ ก็เลือกเอาว่าต้องการผลลัพธ์แบบไหนไปใช้ต่อ

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *