不能将此差异插入表中

时间:2016-03-07 06:09:58

标签: php mysql sql

这是我的插入部分$ total_minutes和$ total_hour不能插入表中

+=

我怎样才能在表格中添加这个?

  

分钟

-=
  

小时

$year = $_POST['year'];  
$month = $_POST['month'];  
$day = $_POST['day'];  
$hour = $_POST['hour'];  
$min = $_POST['min'];  
$sec = $_POST['sec'];  
$year1 = $_POST['year1'];  
$month1 = $_POST['month1'];  
$day1 = $_POST['day1'];  
$hour1 = $_POST['hour1'];  
$min1 = $_POST['min1'];  
$sec1 = $_POST['sec1'];  
$time_in = $year.'-'.$month.'-'.$day.' '.$hour.'-'.$min.'-'.$sec;  
$time_out = $year1.'-'.$month1.'-'.$day1.' '.$hour1.'-'.$min1.'-'.$sec1;
$total_minutes = $total_min;
$total_hour = $total_hr;

$sql = "INSERT INTO time (year, month, day, hour, min, sec, year1, month1, day1, hour1, min1, sec1, time_in, time_out, total_minutes, total_hour)
    VALUES
    ('$year','$month','$day','$hour','$min','$sec','$year1','$month1','$day1','$hour1','$min1','$sec1','$time_in','$time_out', '$total_minutes', '$total_hour')";

我是新的,我想要一个简单的答案

2 个答案:

答案 0 :(得分:1)

使用MySQL TIMEDIFF()函数来获得答案。 MySQL Function不是PHP,而是以最简单的方式提供结果。

$datetime1 = strtotime($row['time_in']); //year-month-day hr:min:sec timein  
$datetime2 = strtotime($row['time_out']); //year-month-day hr:min:sec timeout  

$timeInterval = mysql_query("SELECT TIMEDIFF($datetime2, $datetime1)");

$timeArray = explode(":", $timeInterval);

希望这可以帮助你:)

答案 1 :(得分:0)

您必须首先以此格式转换datetime1和datetime2:

datetime1 =   2012-01-01   12:00:00;
datetime2 =   2012-01-02   13:00:00;

然后在我的sql insert查询中直接插入以下函数的值:

TIMESTAMPDIFF(HOUR,datetime2,datetime1)//用于小时计算 TIMESTAMPDIFF(MINUTE,datetime2,datetime1)//用于分钟计算

希望这可以帮助你:)