Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Mysql计算过去7天的工作小时总数_Php_Mysql_Cakephp 3.0 - Fatal编程技术网

Php Mysql计算过去7天的工作小时总数

Php Mysql计算过去7天的工作小时总数,php,mysql,cakephp-3.0,Php,Mysql,Cakephp 3.0,要计算过去7天的总工作小时数,我有一天的总工作小时数。我有4项活动,比如学习、上学、玩耍和睡觉。我想计算过去7天在学习和学校花费的总小时数。 我正在研究Cakephp3.x 欢迎使用cakephp或mysql进行任何查询 $driversHos = $this->DriverLogs->DriverLogGraphs->find('all', [ 'conditions' => [ 'OR' => [

要计算过去7天的总工作小时数,我有一天的总工作小时数。我有4项活动,比如学习、上学、玩耍和睡觉。我想计算过去7天在学习和学校花费的总小时数。 我正在研究Cakephp3.x 欢迎使用cakephp或mysql进行任何查询

$driversHos = $this->DriverLogs->DriverLogGraphs->find('all', [
        'conditions' => [
            'OR' => [
                ['DriverLogGraphs.event_type' => 0], // On duty
                ['DriverLogGraphs.event_type' => 1] // driving
            ],
            'DriverLogGraphs.driver_id' => $id,
            'DriverLogGraphs.start_time >=' => $thisMonthFirstDay
        ],
        'contain' => [ 
            'DriverLogs.Companies',
            'DriverLogs.Driver'
        ],
        'fields' => [
            'Driver.id', 
            'Driver.username', 
            'DriverLogs.driver_id',  
            'Companies.id',
            'Companies.name', 
            'DriverLogGraphs.id',  
            'DriverLogGraphs.driver_log_id', 
            'DriverLogGraphs.event_type',  
            'event_name' => "CASE  
                WHEN DriverLogGraphs.event_type = '0' THEN 'ON DUTY' 
                WHEN DriverLogGraphs.event_type = '1' THEN 'DRIVING'  
                WHEN DriverLogGraphs.event_type = '2' THEN 'SLEEPER BIRTH' 
                WHEN DriverLogGraphs.event_type = '3' THEN 'OFF DUTY'  
            END",
            'hos_date'  => "STR_TO_DATE(DriverLogGraphs.start_time,'%Y-%m-%d')",
            'hos_start' => "CONCAT(STR_TO_DATE(DriverLogGraphs.start_time,'%Y-%m-%d'),' ','00:00:00')",
            'hos_end'   => "CONCAT(STR_TO_DATE(DriverLogGraphs.start_time,'%Y-%m-%d'),' ','23:59:59')",

            'today_start' => "CONCAT(CURDATE(), ' ', '00:00:00')",
            'today_end' => "CONCAT(CURDATE(), ' ', '23:59:59')",

            'work_time'         => 'TIMESTAMPDIFF(SECOND, DriverLogGraphs.start_time, DriverLogGraphs.end_time)',
            'hours_work_today'  => 'SUM(TIMESTAMPDIFF(SECOND, DriverLogGraphs.start_time, DriverLogGraphs.end_time))',

            's_time_7_days' => 'CONCAT( STR_TO_DATE( DriverLogGraphs.start_time - INTERVAL 6 DAY,  "%Y-%m-%d" ) ,  "",  "" )',
            'e_time_7_days' => 'CONCAT( STR_TO_DATE( DriverLogGraphs.end_time,  "%Y-%m-%d" ) ,  "",  "" )',
            's_time_8_days' => 'CONCAT( STR_TO_DATE( DriverLogGraphs.start_time - INTERVAL 7 DAY,  "%Y-%m-%d" ) ,  "",  "" )',
            'e_time_8_days' => 'CONCAT( STR_TO_DATE( DriverLogGraphs.end_time,  "%Y-%m-%d" ) ,  "",  "" )',

            'hours_work_last_7_days' => '
                SUM(
                    IF (
                        DriverLogGraphs.start_time >= CONCAT(STR_TO_DATE(DriverLogGraphs.start_time - INTERVAL 6 DAY,  "%Y-%m-%d" ) ,  " ",  "00:00:00" ) 
                        AND 
                        DriverLogGraphs.end_time <= CONCAT(STR_TO_DATE(DriverLogGraphs.end_time,  "%Y-%m-%d" ) ,  " ",  "23:59:59" ), 
                        CASE  
                            WHEN DriverLogGraphs.event_type = "0" THEN TIMESTAMPDIFF(SECOND, DriverLogGraphs.start_time, DriverLogGraphs.end_time) 
                            WHEN DriverLogGraphs.event_type = "1" THEN TIMESTAMPDIFF(SECOND, DriverLogGraphs.start_time, DriverLogGraphs.end_time)  
                            WHEN DriverLogGraphs.event_type = "2" THEN "0" 
                            WHEN DriverLogGraphs.event_type = "3" THEN "0"  
                        END, 
                        ""
                    )
                )',
        ],      
        'group' => [
            'hos_date',
        ],
        'order' => [
            'hos_start' => 'DESC', 
        ]
    ])->hydrate(false)->toArray();
表中必须有字段日期, 用于标识学生的学生id字段, 考虑模型=事件


检查这些链接:如果你还在挣扎,请参阅上面我的问题中的检查我的工作时间最后7天,我正在这样做我已经有开始时间和结束时间以及事件类型0,1,2,3我只需要计算0和1事件类型之和。查看我在过去7天内0和1事件类型的问题总和中所附的我的图像。如果今天是30,则从该月的24到30求和。如果今天是31,那么从31到25求和。我想我现在让你们明白了这一点
$total_worked=$this->Events->find('all', [
 'conditions'=>[
          'date >='=>DATE(DATE_SUB(NOW(), INTERVAL 7 DAY)),
          'student_id' => $student_id
          ]
]);
$StudentWorkingHour='';
foreach($total_worked as $worked){
    $eachStudentWorkingHour=$StudentWorkingHour+($worked['study']+$worked['school'])
}

echo StudentWorkingHour; // Will result the total worked in one week