微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP以负的起始值随时间进行计算

如何解决PHP以负的起始值随时间进行计算

我正在尝试将某些Excel工作表转换为PHP应用程序。 在Excel工作表中,您可以进行时间计算,例如:


起始时间A1(可能为负),每天您添加或减去一定的小时和分钟。

 - A1= -10:59 
 - A2= -7:36 
 - A3= -18:35 (A1 + A2)
   
 - B2= 4:24 
 - B3= -14:11 (A3 + B2) 

依此类推...


这是我在没有成功的情况下测试过的一些代码...

    $startTime = new \DateTime('00:00:00');
    $startVal = new \DateInterval('PT10H59M');
    $startTime->sub($startVal); // -10:59:00

    $timeSpan = new \DateInterval('PT7H36M'); // 7:36:00
    $addTime = $startTime->add($timeSpan); 

PHP解决此问题的最佳方法是什么?我一直在用DateTime进行测试,但这不允许我从负的时间值开始。

希望有人可以帮我

亲切的问候, 约赫姆

解决方法

我写了一个简单的类来处理这个问题。它适用于您给定的示例,但是由于无法在我的Excel版本中得到否定时间,因此我无法进一步测试。让我知道它是否在某个时候失败。

class MyTime
{
  private $positive = true;
  private $hour=0;
  private $minute=0;

  /**
   * MyTime constructor.
   * Split the given time string into hours and minutes and whether it is positive or negative
   *
   * @param string $timeString In the format '-10:59','4:35',or optionally just minutes '24'
   */
  public function __construct($timeString)
  {
    if(!empty($timeString))
    {
      if(strpos($timeString,'-')===0)
      {
        $this->positive = false;
        $timeString = substr($timeString,1);
      }

      $timeParts = explode(':',$timeString);
      if(!empty($timeParts))
      {
        if(count($timeParts) == 1)
        {
          $this->hour = 0;
          $this->minute = intval($timeParts[0]);
        }
        else
        {
          $this->hour = intval($timeParts[0]);
          $this->minute = intval($timeParts[1]);
        }
      }
    }
  }

  public function getHour()
  {
    return $this->hour;
  }

  public function getMinute()
  {
    return $this->minute;
  }

  /**
   * Convert into minutes either negative or positive
   * @return int
   */
  public function inMinutes()
  {
    $minutes = ($this->hour*60)+$this->minute;
    if(!$this->positive)
    {
      $minutes *= -1;
    }
    return $minutes;
  }

  /**
   * Convert back to a string for output
   * @return string
   */
  public function __toString()
  {
    return ($this->positive?'':'-').$this->getHour().':'.str_pad($this->getMinute(),2,'0',STR_PAD_LEFT);
  }

  /**
   * Add on the given time which could be negative
   * @param MyTime $time
   *
   * @return $this
   */
  public function add(MyTime $time)
  {
    $newMinutes = $this->inMinutes() + $time->inMinutes();
    if($newMinutes<0)
    {
      $this->hour = (int) ceil($newMinutes/60);
    }
    else
    {
      $this->hour = (int) floor($newMinutes/60);
    }
    $this->minute = abs($newMinutes-($this->hour*60));

    if($newMinutes<0)
    {
      $this->positive = false;
      $this->hour *= -1;
    }
    else
    {
      $this->positive = true;
    }
    return $this;
  }
}

$time = new MyTime('-10:59');
echo $time."\n";
echo $time->add(new MyTime('-7:36'))."\n";
echo $time->add(new MyTime('4:24'))."\n";
echo $time->add(new MyTime('18:32'))."\n";

$time = new MyTime('10:59');
echo $time."\n";
echo $time->add(new MyTime('-59'))."\n";

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。