Warm tip: This article is reproduced from serverfault.com, please click

php-如何将不同项目的签入/签出的小时和分钟相加

(php - How to sum the hours and minutes with different items checkin/checkout)

发布于 2020-11-30 16:35:57

我们有一个表,称为:

  • 活动

每行具有以下值:

  • ID
  • 类型(签入/签出)
  • 开始(varchar 2020-11-30 15:45)

给定一个特定的日期,例如2020年11月30日,假设我们有多个签入/签出,该如何计算从这一天开始的小时和分钟?

这就是我们想获取整天总和的函数:

 function sum_all_hours_by_day($fxemploye_id, $day){
    $core_settings = Setting::first();

    $hours = 0;
    $date = date_create($day);
    $date_format = date_format($date, 'Y-m-d');

    $date_format_start = $date_format .  " 00:00";
    $date_format_end = $date_format . " 23:59";

    $options = array('conditions' => array('fxemploye_id = ? and start >= ? and start <= ?', $fxemploye_id, $date_format_start,$date_format_end));
    $activities = FxemployeHasActivity::all($options);

    $i = 0;
    $d1 = 0; //Checkin
    $d2 = 1; //Checkout
    foreach(array($activities) as $ibd){

        $date1 = $ibd[$d1]->start;
        $date2 = $ibd[$d2]->end;

        $timestamp1 = strtotime($date1);
        $timestamp2 = strtotime($date2);


        if($timestamp1 == true && $timestamp2 == true){
            $sum = abs((int)$timestamp2 - (int)$timestamp1)/(60*60); 
            $hours = $hours + $sum;
        }
        $d1 = $d1++;
        $d2 = $d2++;
        $i++;
    }


    return $hours;
}
Questioner
Met El Idrissi
Viewed
0
Met El Idrissi 2020-12-02 06:22:57

我已经解决了一种方法,我认为这不是最好的方法,但是假设数据库结构正在发挥作用,那么它就可以作为一个函数来工作。如果有人可以表演,将受到欢迎。

  function sum_all_hours_by_day($fxemploye_id, $day){
    $core_settings = Setting::first();


    //You can debug forcing the $day = '2020-11-19';
    $date = date_create($day);
    $date_format = date_format($date, 'Y-m-d');

    //Adding the time as we don't have that from database
    $date_format_start = $date_format .  " 00:00";
    $date_format_end = $date_format . " 23:59";

    $options = array('conditions' => array('fxemploye_id = ? and start >= ? and start <= ?', $fxemploye_id, $date_format_start,$date_format_end));

    //Getting all checkins between two days as a range
    $activities = FxemployeHasActivity::all($options);

    //We get the $d1 as a first checkin 2020-11-19 08:15
    //We get the $d2 as a first checkout 2020-11-19 13:14

    $i = 0;
    $d1 = 0; 
    $d2 = 1;
    $hours = 0;
   foreach($activities as $ibd){

        $date1 = $aActivities[$d1]->start;
        $date2 = $aActivities[$d2]->end;

        //Convert tot timestamp
        $timestamp1 = strtotime($date1);
        $timestamp2 = strtotime($date2);


        //That's the formula where we can now the range between two dates in seconds
        $sum = abs((int)$timestamp2 - (int)$timestamp1)/3600; 
       
        //Sum that for the next
        $hours = $hours + $sum;

        //Move to the next checkin/checkout in the array $activities
        $d1 = $d1 + 2;
        $d2 = $d2 + 2;
        $i++;
    }

    //Return in hours
    return $hours;
}

由于我们将小时数设置为5.88,因此我们必须将其转换为小时和分钟,我们可以这样做:

 <?php echo sprintf('%02d:%02d', (int) $time, fmod($time, 1) * 60); ?>

谢谢大家!