我有一个应用程序,允许用户输入开始时间和结束时间。如果结束时间是第二天,我需要调整日期。目前有三个输入字段:日期、battery_start和battery_end。
电池时间为24小时格式,不带:,即Hm。我正在检查计算是否最终小于 0、battery_total <0,如果是,我想将battery_end设置为第二天,但要与用户输入字段的时间一起。
/**
* if the flight spans two days, i.e. starts late at night
* and ends early in the morning then adjust datetime
*/
public function adjustFlightTime()
{
//check battery times
if($this->battery_total < 0)
{
$this->battery_end = date('Y-m-d H:m:s', strtotime("$this->battery_end +1 days"));
$this->battery_total = $this->battery_total +24;
}
}
每当我尝试保存新的battery_end时间时,我都会在时间上额外增加 9 分钟,日期是正确的。
DJ
使用 DateTime
类:
$datetime = new DateTime($this->battery_end);
$datetime->add(new DateInterval('P1D'));
$this->battery_total = $datetime->format('Y-m-d H:i:s');
请注意,我还m
更改为i
。
你必须
写Y-m-d H:i:s
,而不是Y-m-d H:m:s
m = 月