-1

I have an app that allows user to put in a start time and end time. If the end time is into the next day I need to adjust the date. There are currently three input fields, date, battery_start and battery_end.

The battery times are in 24 hour format without the :, i.e. Hm. I am checking to see if the calculation ends up being less than 0, battery_total < 0, and if so I want to set the battery_end to the next day, but with the time the user entered into the field.

     /**
     * 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;
        }
    }

Whenever I try and save the new battery_end time I get an additional 9 minutes added to the time, the date is correct.

DJ

4

2 回答 2

2

使用DateTime类:

$datetime = new DateTime($this->battery_end);
$datetime->add(new DateInterval('P1D'));
$this->battery_total = $datetime->format('Y-m-d H:i:s');

请注意,我也已更改mi.

于 2013-09-17T14:52:19.630 回答
1

你必须写Y-m-d H:i:s,而不是Y-m-d H:m:s

m = 月

于 2013-09-17T14:54:06.780 回答