dsfsad089111 2019-01-12 13:57
浏览 35
已采纳

有没有将分钟转换为百分之一小时PHP的功能

I'm building a check in and out system for my work, the employee can set a start time and after his shift he can set the end time. but here is the problem:

If i start at 17:00 till 23:15 I want to show te result as 6.25 hours worked.

For example 15 minutes (¼ hour) equals .25, 30 minutes (½ hour) equals .50, etc.

Googled a lot of this but i can't find a solution for php. Someone who can help me with this? :)

Many thanks.

  • 写回答

1条回答 默认 最新

  • dqsk4643 2019-01-12 14:13
    关注

    There isn't one function, but the DateTimeInterface (http://php.net/manual/en/class.datetimeinterface.php) provides some tools to help, along with some basic maths:

    <?php
    
    const secsInAnHour = 3600;
    const minsInAnHour = 60;
    const hoursInADay = 24;
    const decimalPlaces = 2;
    
    $date1 = new DateTime();
    $date2 = new DateTime();
    $date1->setTime(17, 00);
    $date2->setTime(23, 15);
    
    $diff = $date2->diff($date1);
    
    $convertToHours = $diff->s / secsInAnHour + $diff->i / minsInAnHour + $diff->h + $diff->days * hoursInADay;
    
    
    $hours = round($convertToHours, decimalPlaces);
    
    // Hours between two dateTimes, to 2 dp. 
    echo $hours;
    

    You could put this into a function inside your implementation, if you wanted to (with start and end times as args).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等