简体   繁体   English

PHP Mysql从一天中的两个日期时间计算每天的总飞行小时数

[英]PHP Mysql Calculating total flight hours per day from just two datetimes across day

Row_id Sensor_ID  datetime_takeoff       datetime_landing
1      SFO        2013-09-18 04:34:22    2013-09-19 08:34:22
2      BWI        2013-09-18 04:34:22    2013-09-18 16:55:23 
3      BWI        2013-09-18 20:34:22    2013-09-19 10:34:22   
4      SFO        2013-09-19 15:21:22    2013-09-19 20:34:22   
5      BWI        2013-09-19 20:34:22    2013-09-20 06:15:16   
6      SFO        2013-09-19 23:47:22    2013-09-20 07:59:59   
7      BWI        2013-09-20 11:34:05    2013-09-21 02:05:21   
8      SFO        2013-09-20 10:28:56    NULL   
9      BWI        2013-09-21 04:09:57    NULL         

This is for PHP and Mysql 这适用于PHP和Mysql
These are airplane takeoff and landing datetime's for two planes SFO and BWI. 这些是SFO和BWI两架飞机的飞机起飞和降落日期时间。 I'm trying to calculate how many hours per day each plane is in the air. 我正在计算每架飞机每天有多少小时。 Flight duration is easy just DATETIMEDIFF but hours per day in the air is proving difficult. 飞行时间很容易,只有DATETIMEDIFF,但空中每天的时间很难证明。

I figure I can break it down into 2 steps: 我想我可以分解为两步:
1. For any take off and landing pair in the same day just get the datetimediff and if datetime_takeoff and datetime_landing are not in the same day then insert the total amount of hours from mindnight. 1.对于同一天的任何起飞和降落对,只需获取datetimediff,如果datetime_takeoffdatetime_landing不在同一天,则插入整夜的总小时数。 I can run this as it occurs. 我可以在它发生时运行它。



2. Run a check every day at midnight and check if the datetime_landing is NULL and IF the datetime_takeoff is TODAY, THEN calculate the hours to midnight. 2.每天午夜运行检查,检查datetime_landing是否为NULL,如果datetime_takeoff是TODAY,那么计算午夜的小时数。 IF datetime_takeoff is not today then insert a value of 24hrs. 如果datetime_takeoff不是今天,则插入24小时的值。 I figure this will cover timedate_landings that are not on the same day/NULL 我想这将涵盖不在同一天/ NULL的timedate_landings

Does this logic make sense where all the data will be processed? 在处理所有数据的情况下,这种逻辑是否有意义?

Maybe something like this? 也许是这样的?

select
    sensor_ID,
    day,
    sum(flighttime) as sumflighttime
from
(
    (select
        sensor_ID,
        date_format(datetime_takeoff, '%Y-%m-%d') as day,
        case
            when date_format(datetime_takeoff, '%d') = date_format(datetime_landing, '%d')
                then subtime(datetime_landing, datetime_takeoff)
            else subtime(date_format(datetime_landing, '%Y-%m-%d'), datetime_takeoff)
        end flighttime
    from
        datatable
    ) b
    union all
    (select
        sensor_ID,
        date_format(datetime_landing, '%Y %m %d') as day,
        subtime(datetime_landing, date_format(datetime_landing, '%Y-%m-%d')) flighttime
    from
        datatable
    where
        date_format(datetime_takeoff, '%d') != date_format(datetime_landing, '%d')
    ) b
) t
group by
    sensor_ID,
    day

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM