$arr = array('0:16:0', '25:12:5', '0:0:10', '0:5:0');
// converting all the times to seconds
$t_seconds = 0;
foreach ($arr as $v) {
sscanf($v, "%d:%d:%d", $hours, $minutes, $seconds);
$t_seconds += $hours * 3600 + $minutes * 60 + $seconds;
}
// condition if seconds calculated are negative
$sign = ($t_seconds < 0 ? '-' : '');
$t_seconds = abs($t_seconds);
// converting seconds, taking care of wrong minutes/seconds formats like 02:63:65
$hours = floor($t_seconds / 3600);
$minutes = floor(($t_seconds / 60) % 60);
$seconds = $t_seconds % 60;
// final format
$sum = $sign . sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
输出:
25:33:15
答案 0 :(得分:3)
输入的值无效。使用PreparedStatement
设置查询中的值。在sql中使用应插入值的列名。主键默认在MySQL中自动生成,因此您无需使用它。最后,您也不需要指定;
。执行语句时,它会自动添加结束字符。
//SQL Query to insert user input
String sql = "insert into tab(name, age) values(?,?)";
Statement st = con.prepareStatement(sql);
st.setString(1, name);
st.setInt(2, age);
//Execute INSERT
st.executeUpdate();