通过查询插入的时间会在几分钟后显示更多数字

时间:2019-07-08 15:17:21

标签: html sql

我正在将时间字段插入到表中,其中该时间字段在数据库中的长度为6。

我在表单中使用Tempos Dominus Bootstrap库

HTML表单

<div class="form-group">
                    <label for="time">Hora</label>
                        <div class="input-group date" id="datetimepicker3" data-target-input="nearest">
                            <input type="text" name="time" class="form-control datetimepicker-input" data-target="#datetimepicker3"/>
                            <div class="input-group-append" data-target="#datetimepicker3" data-toggle="datetimepicker">
                                <div class="input-group-text"><i class="fa fa-clock-o"></i></div>
                            </div>
                        </div>
                    </div>

SQL查询

$time = strtotime($_POST['time']);
    $time = date('HH:mm', $time);


    //insere novo turno na base de dados
    $sql = "INSERT INTO shifts (name, origin, destination, transport_number, status, date, time) VALUES (?,?,?,?,?,?,?)";
    $stmt = mysqli_stmt_init($conn);
    if (!mysqli_stmt_prepare($stmt, $sql)) {
        header("Location: ../shifts.php?error=sqlerror");
        exit();
    } else {
        mysqli_stmt_bind_param($stmt, "sssssss", $username, $origin, $destination, $transport, $status, $date, $time);
        mysqli_stmt_execute($stmt);
        header("Location: ../shifts.php?newrecord=success");
        exit();

    }

我尝试使用H:i:s格式,但似乎不起作用。

预期结果

11:07:00

实际结果

11:07:00.000000

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

列的格式为TIME(6)。例如,here

  

TIME(N)是一列,其中N是代表整数的整数   小数部分(秒),最多6位数字。

或者换句话说,TIME(6)表示点后的六个零。我相信要获得结果,您的列应具有TIME(0)或仅TIME作为数据类型,否则您将必须舍弃零。