请帮我转换导致错误的字符串!
PHP
$string_date = "2015-04-16 12:00:00";
$date = new DateTime($string_date);
//Put breaks in array
$usr_breaks = $stmt2->fetch();
//Put break 1 in variable
$usr_break_1 = $usr_breaks['skift_rast1'];
//Split usr_break 1 to calculate duration
list($break_1_start, $break_1_ends) = explode("-", $usr_break_1);
// (09:00, 09:20) = explode "-", 09:00-09:20
//Create Datetime objekt
$break_1_start = $date->format('Y-m-d ').$break_1_start;
$break_1_ends = $date->format('Y-m-d ').$break_1_ends;
//Calculate break 1 duration
$break_1_dur = $break_1_start->diff($break_1_ends); //This line is error
echo $break_1_dur->format('H:i');
答案 0 :(得分:1)
使用DateTime :: createFromFormat();
解决它//Create Datetime objekt
$break_1_start = $date->format('Y-m-d ').$break_1_start;
$break_1_ends = $date->format('Y-m-d ').$break_1_ends;
$break_1_start = DateTime::createFromFormat('Y-m-d H:i', $break_1_start);
$break_1_ends = DateTime::createFromFormat('Y-m-d H:i', $break_1_ends);
//Calculate break 1 duration
$break_1_dur = $break_1_start->diff($break_1_ends);
echo $break_1_dur->format('%h:%i');