Php 在数据库中使用时间进行计算

Php 在数据库中使用时间进行计算,php,database,mysqli,Php,Database,Mysqli,我有一个colums[id,Boat,Stop,Time_D]数据库,其中Boat是船的类型,Stop是停靠的次数,Time_D是船的出发时间。 到目前为止,数据库运行得非常顺利,我为stop 1插入了两个样本时间和船只 但是我现在想用这些时间来插入stop2的时间,因为时间差 停止1和停止2的时间为10分钟 我当然不想再次手动输入stop 1的大量发车时间,我想我可以做得更聪明一些 这些新时间可以放在同一个表中,在stop 1的时间之下,或者在一个单独的表中,然后我不再需要列stop了当然,我

我有一个colums[id,Boat,Stop,Time_D]数据库,其中Boat是船的类型,Stop是停靠的次数,Time_D是船的出发时间。 到目前为止,数据库运行得非常顺利,我为stop 1插入了两个样本时间和船只

但是我现在想用这些时间来插入stop2的时间,因为时间差 停止1和停止2的时间为10分钟

我当然不想再次手动输入stop 1的大量发车时间,我想我可以做得更聪明一些

这些新时间可以放在同一个表中,在stop 1的时间之下,或者在一个单独的表中,然后我不再需要列stop了当然,我真的不在乎它是如何工作的


到目前为止,我一直在使用MySQLi和PHP,我希望这也适用于此。

我自己解决了这个问题,请参见代码中的注释

$time_difference=600; //time difference in departure times between the two stops
$stop_old = "1"; //stop from which to use the times to calculate the next times
$stop_new = "2"; //the new stop


//get times from stop in array
$result = mysqli_query($con,"SELECT `Boat`, `Time_D` from `Schedule` where `Stop` = '".$stop_old."' ");
//get database result
while ($row = mysqli_fetch_array($result) ) {                         
$rows[] = $row['Time_D']; //store all the times in one array
$boats[] = $row['Boat']; // store all the boats in one array
}
//print_r ($rows); //used for debugging
//Iterate over the array to change all the elements
for($i = 0; $i < sizeof($rows); $i++){
$rows[$i] = ( strtotime($rows[$i]) ) + ($time_difference); //with timestamps, because, well, php and times...
$rows[$i] = date('H:i:s', ($rows[$i]) ); // back from timestamp to time
}
//print_r($rows);
//print_r($boats);

for($i = 0; $i < sizeof($rows); $i++){
    mysqli_query($con, "INSERT INTO `Schedule`(`Boat`, `stop`, `Time_D`) VALUES ('$boats[$i]', '".$stop_new."', '$rows[$i]')");
}

mysql中的行间计算可能非常痛苦。在客户端php代码中执行。^非常同意,php启动人。@Marc B好的,所以我把它放在一个数组中,然后加上10分钟,然后以某种方式放回去?差不多。从表中选择*进入数组,执行$row[$i+1]['time\U d']=$row[$i]['time\U d']+$time\U difference,或任何计算,然后迭代数组并用更改的值更新db。谢谢,我会尝试的。如果我不成功,我会回到这里。