需要计算经过一段“时间”的时间;移位窗口”;(PHP)


Need to calculate elapsed time over a "shifting window" (PHP)

我需要计算24小时和48小时内的多个睡眠事件。

时间计算本身没有问题,而是如何处理滚动周期。

我所说的"滚动期"如下:

轮班工人的睡眠记录为开始时间经过的秒数。这很容易管理。

但我需要记录的是,在特定时间(例如下午4点)之前的24小时和48小时内,轮班工人的睡眠时间。但具体时间各不相同。

因此,我们的轮班工人在9月1日01:00至08:00睡觉,休息一天。她于9月1日22:00至9月2日06:00再次睡觉,并于9月2号06:30开始工作。

我需要算法来计算她在早上6:30轮班前24小时(即9月1日6:30以来)和之前48小时的睡眠时间。

我甚至想不出如何简单地描述这一点,更不用说制定一个算法了。

我所能想到的就是有一个由48个元素组成的数组,每个元素代表一个小时,在记录睡眠时,用每个睡眠小时的部分填充每个数组,然后每小时滚动一次数组。

交给智囊团?

php中处理日期或时间计算的最简单方法是使用DateTime对象。我必须对您的数据格式做出一些假设,但根据我的经验,这种设置很常见。

<?php
/** Your data */
$sleepCycles = array(
    /** Dates as YYYY-MM-DD and times as HH:ii:ss  */
  array('sleepid'=>1,'startdate'=>'2014-01-01','starttime'=>'08:00:00','lengthinseconds'=>28800),
  array('sleepid'=>2,'startdate'=>'2014-01-05','starttime'=>'17:00:00','lengthinseconds'=>28800)
    );
/** The start and end of your rolling check period. 
 * $hoursToCheck and $start should be input.  
 * $start should be in format "YYYY-mm-dd HH:ii:ss"
 */
$rollStart = new DateTime( $start );  
$rollEnd = new DateTime( $start );
/**  $hoursToCheck should be a number or a string that resolves to a number.  */
$rollEnd->modify('+'.$hoursToCheck.' hour');

/** The counter for how much sleep falls within your check */
$amountSleptInSeconds = 0;  
foreach ( $sleepCycles as $sc )
{
  $startDateObj = new DateTime( $sc['startdate'] .' '. $sc['starttime'] );
  $endDateObj = new DateTime( $sc['startdate'] .' '. $sc['starttime'] );
  $endDateObj->modify('+'.$sc['lengthinseconds'].' second');
  /**
   * If either the start or end of the sleep cycle fall 
   * inside the rolling check period,
   * some part of the sleeping counts.
   */
  if ( $rollStart <= $startDateObj && $startDateObj >= $rollEnd )
  {
    /** Get the time that counts. Note it may not be the entire sleep period. 
     * If both ends of the sleep also falls inside the check, count the entire
     * length.  If not, only count the sleep that is inside.
     */
    if ( $endDateObj >= $rollEnd )
    {  $amountSleptInSeconds += $sc['lengthinseconds'];  }
    else
    {  
      $countMeInterval = $startDateObj->diff($rollEnd , TRUE);  
      $amountSleptInSeconds += $countMeInterval->format('%s');
    }
  }
  else if ( $rollStart <= $endDateObj && $endDateObj >= $rollEnd )
  {
    if ( $rollStart <= $startDateObj )
    {  $amountSleptInSeconds += $sc['lengthinseconds'];  }
    else
    {
      $countMeInterval = $rollEnd->diff($startDateObj , TRUE);
      $amountSleptInSeconds += $countMeInterval->format('%s');
    }
  }
}
print 'Within time period '
    .$rollStart->format('M j, Y g:ia').' - '
    .$rollEnd->format('M j, Y g:ia')
    .' (inclusive of end points) I found this amount of seconds of sleep: '
    .$amountSleptInSeconds;
?>