php 如何在我的 mysql 表中插入时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37616760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:18:35  来源:igfitidea点击:

How to insert timestamp into my mysql table

phpmysql

提问by johnbumble

In the mysql table I have a field called date its type is called timestamp and the default is CURRENT_TIMESTAMP. However, if I leave the field blank in mysql I get an error. When I try to insert something into it like time() I receive the date as 0000-00-00 00:00:00.

在 mysql 表中,我有一个名为 date 的字段,其类型称为时间戳,默认值为 CURRENT_TIMESTAMP。但是,如果我在 mysql 中将该字段留空,则会出现错误。当我尝试像 time() 这样插入一些东西时,我收到的日期为 0000-00-00 00:00:00。

  <?php
    $name         = "";
    $email        = "";
    $subject      = "";
    $comments     = "";
    $nameError    = "";
    $emailError   = "";
    $subjectError = "";
    $x            = 5;
    function filterData($data)
    {
        $data = htmlspecialchars($data);
        $data = stripslashes($data);
        return $data;
    }
    $connection = mysql_connect('host', 'user', 'pass');
    if (!$connection) {
        die('Could not connect: ' . mysql_error());
    }


    $select_database = mysql_select_db("contact");

    if (!$select_database) {
        echo "could not select database " . mysql_error();

    }
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        //handles the name 
        $name = filterData($_POST["name"]);
        if (empty($name)) {
            $nameError = "please don't leave the name field blank";
        }
        //handles the email
        $email = filterData($_POST["email"]);
        if (empty($email)) {
            $emailError = "please don't leave the email field blank";
        }

        //handles the subject
        $subject = filterData($_POST["subject"]);
        if (empty($subject)) {
            $subjectError = "please don't leave this field blank";
        }

        $comments = filterData($_POST["comments"]);

    }

    $insertation = "INSERT INTO contactinfo (name, email, subject, date, comments)
        VALUES ('$name', '$email', '$subject', '', '$comments')";

    $insertationQuery = mysql_query($insertation, $connection);

    if (!$insertationQuery) {
        echo "Could not process your information " . mysql_error();
    } else {
        echo "Thank you for submitting the information";
    }

    ?>

回答by shamsup

In addition to checking your table setup to confirm that the field is set to NOT NULLwith a default of CURRENT_TIMESTAMP, you can insert date/time values from PHP by writing them in a string format compatible with MySQL.

除了检查您的表设置以确认该字段设置为NOT NULL默认值之外CURRENT_TIMESTAMP,您还可以通过以与 MySQL 兼容的字符串格式编写日期/时间值来插入 PHP 中的日期/时间值。

 $timestamp = date("Y-m-d H:i:s");

This will give you the current date and time in a string format that you can insert into MySQL.

这将以字符串格式为您提供当前日期和时间,您可以将其插入到 MySQL 中。

回答by SonDang

Please try CURRENT_TIME()or now()functions

请尝试CURRENT_TIME()now()功能

"INSERT INTO contactinfo (name, email, subject, date, comments)
VALUES ('$name', '$email', '$subject', NOW(), '$comments')"

OR

或者

"INSERT INTO contactinfo (name, email, subject, date, comments)
VALUES ('$name', '$email', '$subject', CURRENT_TIME(), '$comments')"

OR you could try with PHP datefunction here:

或者你可以在这里尝试使用 PHPdate函数:

$date = date("Y-m-d H:i:s");

回答by Hamza Zafeer

$insertation = "INSERT INTO contactinfo (name, email, subject, date, comments)
VALUES ('$name', '$email', '$subject', CURRENT_TIMESTAMP, '$comments')";

You can use this Query. CURRENT_TIMESTAMP

您可以使用此查询。CURRENT_TIMESTAMP

回答by Abu Shoeb

You do not need to insert the current timestamp manually as MySQLprovides this facility to store it automatically. When the MySQLtable is created, simply do this:

您不需要手动插入当前时间戳,因为MySQL此工具提供了自动存储它的功能。当MySQL创建表,只要做到这一点:

  • select TIMESTAMPas your column type
  • set the Defaultvalue to CURRENT_TIMESTAMP
  • then just insertany rows into the table without inserting any values for the timecolumn
  • 选择TIMESTAMP作为您的列类型
  • Default值设置为CURRENT_TIMESTAMP
  • 然后只是insert表中的任何行而不为time列插入任何值

You'll see the current timestampis automatically inserted when you insert a row. Please see the attached picture. enter image description here

current timestamp当您插入一行时,您会看到自动插入。请看附图。在此处输入图片说明

回答by salvatore

The DEFAULTvalue of a column in MySql is used only if it isn't provided a value for that column. So if you

DEFAULT,如果它不提供用于该列的值MySQL中的列的值仅使用。所以如果你

INSERT INTO contactinfo (name, email, subject, date, comments)
VALUES ('$name', '$email', '$subject', '', '$comments')

You are not using the DEFAULTvalue for the column date, but you are providing an empty string, so you get an error, because you can't store an empty string in a DATETIMEcolumn. The same thing apply if you use NULL, because again NULLis a value. However, if you remove the column from the list of the column you are inserting, MySql will use the DEFAULTvalue specified for that column (or the data type default one)

您没有使用DEFAULTcolumn的值date,但是您提供了一个空字符串,因此您会收到错误消息,因为您无法在DATETIME列中存储空字符串。如果您使用NULL,同样适用,因为再次NULL是一个值。但是,如果从要插入的列的列表中删除该列,MySql 将使用DEFAULT为该列指定的值(或数据类型默认值)

回答by MikeGsus

You can try wiht TIMESTAMP(curdate(), curtime())for use the current time.

您可以尝试使用TIMESTAMP(curdate(), curtime())来使用当前时间。