无法在MySQL-PHP中的数据库列datetime类型中存储值

时间:2017-11-06 03:59:20

标签: php mysql

我想在数据库列中保存并存储来自$ interval的日期格式,它具有数据时间类型。但它不起作用,在数据库中没有显示出价值。

 if ($no > 1){
     $startdate = new DateTime($lup[$no]);
     $enddate = new DateTime($lup[$no-1]);
     $int = $startdate->diff($enddate);
     $interval = $int->format("%d days, %h hours, %i minutes, and %s seconds  ");
     echo "<td>".$interval."&nbsp;</td>";
    }else
    {
     echo "<td> --- </td>&nbsp;";
    }

 if ($data['id'] == 2){
  $sql = mysql_query("
  INSERT INTO sla (booking_id, approval)
  VALUES ('$booking_id[$no]', '$interval')")
 ;}

3 个答案:

答案 0 :(得分:1)

试试这个:

if ($no > 1){
    $startdate = new DateTime($lup[$no]);
    $enddate = new DateTime($lup[$no-1]);
    $int = $startdate->diff($enddate);
    $interval = $int->format("%d days, %h hours, %i minutes, and %s seconds &nbsp;");
    $dtInterval = $int->format('Y-m-d H:i:s');
    echo "<td>".$interval."&nbsp;</td>";
   }else
   {
    echo "<td> --- </td>&nbsp;";
   }

if ($data['id'] == 2){
 $sql = mysql_query("
 INSERT INTO sla (booking_id, approval)
 VALUES ('$booking_id[$no]', '$dtInterval')")
;}

答案 1 :(得分:0)

试试这个

if ($no > 1){
    $startdate = new DateTime($lup[$no]);
    $enddate = new DateTime($lup[$no-1]);
    $int = $startdate->diff($enddate);
    $interval = $int->format("%d days, %h hours, %i minutes, and %s seconds &nbsp;");
    echo "<td>".$interval."&nbsp;</td>";
   }else
   {
    echo "<td> --- </td>&nbsp;";
   }

if ($data['id'] == 2){
 $sql = mysql_query("
 INSERT INTO sla (booking_id, approval)
 VALUES ('".$booking_id[$no]."', '".$interval."')")
;}

答案 2 :(得分:0)

$interval值是一个字符串。如果您希望以该格式存储数据,则应将审核列的数据类型从 sla 表更改为 varchar ,而不是的日期时间