Функция контроллера для вставки времени:
$input = $this->input->post('shiftStartTime');
$input = str_replace(" : ", ":", $input);
$shiftstarttime = date('H:i:s', strtotime($input));
$input2 = $this->input->post('shiftEndTime');
$input2 = str_replace(" : ", ":", $input2);
$shiftEndTime = date('H:i:s', strtotime($input2));
Я хочу взять разницу во времени в 2 раза
Вот мой код, но он показывает неверную разницу во времени. Как его решить?
$input3 = $this->input->post('shiftEndTime')-$this->input->post('shiftStartTime');
$input3 = str_replace(" : ", ":", $input3);
$duration = date('H:i:s', strtotime($input3));
@ Билал Ахмед, как
@ Saclt7, я добавил ответ с более подробной информацией
какой формат у ваших дат shiftStartTime и shiftEndTime?






Существует несколько способов рассчитать разницу между двумя временами.
// example 1
$time1 = "08:00:00";
$time2 = "13:40:00";
echo "Time difference: ".get_time_difference($time1, $time2)." hours<br/>";
// example 2
$time1 = "22:00:00";
$time2 = "04:00:00";
echo "Time difference: ".get_time_difference($time1, $time2)." hours<br/>";
function get_time_difference($time1, $time2)
{
$time1 = strtotime("1/1/1980 $time1");
$time2 = strtotime("1/1/1980 $time2");
if ($time2 < $time1)
{
$time2 = $time2 + 86400;
}
return ($time2 - $time1) / 3600;
}
Здесь разница во времени хранится в базе данных как 00:00:00.
print_r($this->input->post('shiftStartTime')) распечатайте эту строку и покажите мне результат
Теперь разница показана, но это неправильное значение
Результат - 3:14 утра.
попробуйте с этой функцией diff
<?php
$input = $this->input->post('shiftStartTime');
$input = str_replace(" : ", ":", $input);
$shiftstarttime = date('H:i:s', strtotime($input));
$input2 = $this->input->post('shiftEndTime');
$input2 = str_replace(" : ", ":", $input2);
$shiftEndTime = date('H:i:s', strtotime($input2));
echo date_create($shiftEndTime)->diff(date_create($shiftstarttime))->format('%H:%i:%s');
?>
use below code to find the time difference between two times.
$input = $this->input->post('shiftStartTime');
$input = str_replace(" : ", ":", $input);
$input2 = $this->input->post('shiftEndTime');
$input2 = str_replace(" : ", ":", $input2);
$date_a = new DateTime($input);
$date_b = new DateTime($input2);
$interval = date_diff($date_a,$date_b);
$diff = $interval->format('%h:%i:%s');
использовать функцию
date_diff()