douxing1850
douxing1850
2017-11-06 03:59
浏览 38

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

I wanna save and store date format from $interval in database column which have dattime type. But its not work, in database not show that value.

 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条回答 默认 最新

  • doqdcks3948
    doqdcks3948 2017-11-06 05:38

    Try this

    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."')")
    ;}
    
    点赞 评论
  • dongyanhu5628
    dongyanhu5628 2017-11-06 06:33

    The $interval value is a string. If you want to store the data in that format then you should change the data type of your approval column from your sla table into varchar instead of datetime.

    点赞 评论
  • doudong2149
    doudong2149 2017-11-06 06:33

    Try this:

    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')")
    ;}
    
    点赞 评论

相关推荐