2013-02-19 37 views
0

我正在尝试创建一个函数来计算2次之间的时间差,但只包括工作时间。 这是一个追踪工作完成所需时间的项目。PHP - 仅使用工作时间的日期/时间差

工作时间为周一至周五上午9点至下午5点。

  • 例1
    如果工作开始于下午4:50,有人第二天会根据网页上午10时,计时器会说1H10米0。
  • 例子2
    如果工作从下午6点23分开始,有人在第二天上午9点30分看到页面,计时器会说30米0秒。
  • 示例3
    如果工作从周五下午1:20开始,有人在周四下午4点看到页面,则计时器会说18h 40m 0s。 (它排除星期六和星期天!)
+0

查看本页面的回答 [链接](http:// stackoverflow。com/questions/2091838 /时间差之间的2日期忽略周末时间) – 2013-02-19 13:47:42

+0

@彼得我试图循环通过for循环的每个小时,并试图缩小它与if语句,但它只是当我进入分钟和秒钟时,会变得混乱和混乱。如果你能告诉我你将如何去做,我现在真的有一个精神障碍,我会非常感激。 – Clarkey 2013-02-19 13:54:50

+0

@AmineMatmati我看了一下,这更侧重于跳过周末而不是工作时间以外的所有事情。 – Clarkey 2013-02-19 14:00:05

回答

1

这可能有点压倒性,但在链接@Amine Matmati提供了你所需要的所有工具。有了它,您可以轻松地从开始时间戳到现在,每天都可以遍历,并且可以跳过周末和休息时间。

这有点笨重,但它会工作。

strtotime(str, timestamp)需要一个字符串,它是一个几乎简单的英语命令,如果你想要,一个参考时间戳。这是您将用于从当前时间戳一直运行到结束时间戳的内容。

即。 strtotime('end of the working day', timestamp of when the job started);

[注:“一天的结束”是不是一个可以接受的字符串,检查链接到PHP手册在底部,我只是想让你知道它是如何对你有用]

所以,如果你建立了一个从原始时间标记走到现在的循环,你可以计算你工作时间的数量。

我会告诉你在伪代码的逻辑:在工作时间

$Present is present timestamp; 
$Start is the starting timestamp; 
$Hours will be the accumulated working hours between starting timestamp and present timestamp; 

loop while present timestamp not reached { 

if $Start does not occur on the same day as $Present{ 
    if $Start occurs inside of working hours { 
    find the time to the end of the working day; 
    add that time to $Hours; 
    } 

    find next day; 
    if the next day is Saturday{ 
    update $Start to the working start of next Monday; 
    } 
    else{ 
    update $Start to the working start of the next day; 
    } 
} 
else{ 
    find the time between $Start and $Present; 
    add that time to $Hours; 
    update $Start to $Present or Break; 
    } 
} 

它(即前)假设起始时间不会在周末发生,并且不会发生外当前时间戳的同一天,但控制这些是非常容易的。

东西,这将成为你的朋友在这个有:

@Amine Matmati的链接:Time difference between 2 dates ignoring weekend time

PHP日期():http://php.net/manual/en/function.date.php
PHP的strtotime():http://www.php.net/manual/en/function.strtotime.php

编码愉快!