PHP使用负起始值随时间计算
我正在尝试将某个excel工作表转换为php应用程序。 在excel工作表中,您可以进行时间计算,例如:PHP使用负起始值随时间计算,php,excel,datetime,time,Php,Excel,Datetime,Time,我正在尝试将某个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');
开始时间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,但这不允许我从负时间值开始
希望有人能帮我一把
亲切问候,,
Jochem我编写了一个简单的类来处理这个问题。它适用于给定的示例,但我无法进一步测试,因为在我的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";
class MyTime
{
private$positive=true;
私人$hour=0;
私人$minute=0;
/**
*MyTime构造函数。
*将给定的时间字符串拆分为小时和分钟,以及它是正还是负
*
*@param string$timeString格式为“-10:59”、“4:35”或可选的分钟数“24”
*/
公共函数构造($timeString)
{
如果(!empty($timeString))
{
if(strpos($timeString,'-')==0)
{
$this->positive=false;
$timeString=substr($timeString,1);
}
$timeParts=explode(“:”,$timeString);
如果(!空($timeParts))
{
如果(计数($timeParts)==1)
{
$this->hour=0;
$this->minute=intval($timeParts[0]);
}
其他的
{
$this->hour=intval($timeParts[0]);
$this->minute=intval($timeParts[1]);
}
}
}
}
公共函数getHour()
{
返回$this->hour;
}
公共函数getMinute()
{
返回$this->minute;
}
/**
*将分钟转换为负数或正数
*@return int
*/
公众活动(分钟)
{
$minutes=($this->hour*60)+$this->minutes;
如果(!$this->正)
{
$minutes*=-1;
}
返回$minutes;
}
/**
*转换回字符串以进行输出
*@返回字符串
*/
公共函数
{
返回($this->getMinute(),2,'0',stru pad_左);
}
/**
*加上可能为负的给定时间
*@param MyTime$time
*
*@return$this
*/
公共函数添加(MyTime$time)
{
$newMinutes=$this->inMinutes()+$time->inMinutes();
如果($newMinuteshour=(int)ceil($newMinutes/60);
}
其他的
{
$this->hour=(int)楼层($newMinutes/60);
}
$this->minute=abs($newMinutes-($this->hour*60));
如果($newMinutespositive=false;
$this->hour*=-1;
}
其他的
{
$this->positive=true;
}
退还$this;
}
}
$time=新的MyTime('-10:59');
回显$time。“\n”;
echo$time->add(新MyTime('-7:36'))。“\n”;
echo$time->add(新MyTime('4:24'))。“\n”;
echo$time->add(新MyTime('18:32'))。“\n”;
$time=新MyTime('10:59');
回显$time。“\n”;
echo$time->add(新MyTime('-59'))。“\n”;
我会将时间转换为秒,求和(取决于符号+或-),然后再转换为分钟。嘿,马克!我真的非常感谢你所创造的一切!非常感谢!不过我想我偶然发现了一个小错误。例如:$time=new MyTime('-24:59');echo$time->add(new MyTime('+12:00')。“\n”echo$time->add(new MyTime->new MyTime('+12:00'))。“\n”echo$time->add(“+12:00”)。“\n”echo$time。”\n“
这给出了“0:59”,但它应该是“-0:59”。知道原因吗?我已经更正了代码。问题出在add函数中。设置新的小时和分钟后,我正在检查$This->getHour()马克,我又发现了一个bug,但做了一个小的修复:当分钟数(例如'08*minutes)的小数点只会将'8'设置为分钟数时:我添加了这段代码来修复它,可能不是最干净的,但它可以工作。在Add函数中,$minutes=abs($newMinutes-($this->hour*60));$this->minute=strlen($分钟)==1?0。$minutes:$minutes;
如果你已经让它工作了,这很好,但是hours和minutes应该保持为整数类型,直到类被转换为带有u-toString的字符串,然后它会在前导零前加上前缀。这是因为对它们的所有计算。我不确定我是否理解你提到的bug,也不知道你的修复方法是什么应该这样做。如果要将$this->minutes转换为字符串,PHP每次需要它进行计算时都必须将其转换回整数。嗨,马克,很抱歉解释得不好,让我给你一个例子$time=new MyTime('-6:32');echo$time。“\n”;echo$time->add(new MyTime('-7:36')。“\n”“;echo$time。”\n“
这次返回“-14:80”,但它应该是“-14:08”。希望这能消除一些混淆