doujin8476 2015-12-02 12:50
浏览 45
已采纳

计算一天中的工作时间与应该工作的小时数

How to a calculate hours that are worked in a day?

Employee should work 8 hours so  8 x 3600 = 28,800 second lets say the employee only works for 4 hours 4 x 3600 = 14,400 so the result would be 14,400 - 28,800 = -14,400 so how do i convert the result to hours and minutes and seconds
RESULT =  - 04:00:00

also lets say 8 hours should work 8 x 3600 = 28,000 seconds lets say the employee works 12 hours 12 x 3600 = 43,200 = 43,200 seconds result would be 43,200 - 28,000 = 15,200
RESULT =  + 04:00:00

  <?php 
$hoursworked = 4*3600
$dd = 8*3600;
                        echo    gmdate("H:i:s", $dd%86400);     

            // day total            
        $ee =$hoursworked - $dd ;
                $seconds = $ee; 

        $hours = floor($seconds / 3600);
        $mins = floor(($seconds - $hours*3600) / 60);
        $s = $seconds - ($hours*3600 + $mins*60);

        $mins = ($mins<10?"0".$mins:"".$mins);
        $s = ($s<10?"0".$s:"".$s); 

        $timea = ($hours>0?$hours.":":"").$mins.":".$s; ?>

The result I would like from the above example is -04:00:00 because the employee as only worked 4 hours and should have worked 8 Thanks

  • 写回答

2条回答 默认 最新

  • douzhang3356 2015-12-02 13:10
    关注

    Try the below code once.

    function calc($work_hr, $tot_hr)
    {
        $tot = $tot_hr * 3600;
        $work = $work_hr * 3600;
        if ($tot > $work) {
            $seconds = $tot - $work;
            $total = "- " . gmdate("H:i:s", $seconds);
        } else {
            $seconds = $work - $tot;
            $total = "+ " . gmdate("H:i:s", $seconds);
        }
        return $total;
    }
    echo "Less Hours :" . calc(4,8);  //Output : - 04:00:00
    echo "<br>";
    echo "Extra Hours:" . calc(12,8); //Output : + 04:00:00
    echo "<br>";
    echo "Extra Hours:" . calc(9.5,8); //Output : + 01:30:00
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度