php 在 mySQL 中存储时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6519968/
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 00:37:36  来源:igfitidea点击:

Storing timestamps in mySQL

phpmysqltimestamp

提问by medk

I remember I read somewhere that mySQL timestamp data type is used to be updated automatically when the row updates, is this true?

我记得我在某处读到过 mySQL 时间戳数据类型用于在行更新时自动更新,这是真的吗?

Do I have to store timestamps with timestamp datatype or with bigint for example?

例如,我是否必须使用时间戳数据类型或 bigint 存储时间戳?

NB: the timestamp that has to be stored is not generated by mySQL, it's a pre-defined value that will be generated with PHP.

注意:必须存储的时间戳不是由 mySQL 生成的,而是由 PHP 生成的预定义值。

Thanks.

谢谢。

回答by Brendan Bullen

A column of type TIMESTAMPcan be set to update automatically using ON UPDATE CURRENT_TIMESTAMPin the column definition.

TIMESTAMP可以将类型的列设置为ON UPDATE CURRENT_TIMESTAMP在列定义中使用自动更新。

However, this will not populate on insert of a record. As you have stated, you can build a timestamp in PHP (format: YYYY-MM-DD HH:MM:SS) and insert it, or you can set another column attribute of DEFAULT CURRENT_TIMESTAMPin the column definition.

但是,这不会在插入记录时填充。正如您所说,您可以在 PHP 中构建时间戳(格式:)YYYY-MM-DD HH:MM:SS并插入它,或者您可以DEFAULT CURRENT_TIMESTAMP在列定义中设置另一个列属性。

For more information: MySQL: TIMESTAMP Properties

有关更多信息:MySQL:时间戳属性

UPDATE:If a unix timetstamp value is the required output, store the value using TIMESTAMPand then get the unix value using:

更新:如果 unix 时间戳值是所需的输出,请使用以下方法存储该值TIMESTAMP,然后使用以下方法获取 unix 值:

SELECT UNIX_TIMESTAMP(your_column_name) FROM your_table_name

To insert using a unix timestamp, you can use FROM_UNIXTIME()

要使用 unix 时间戳插入,您可以使用 FROM_UNIXTIME()

INSERT INTO (your_table_name) (your_column_name) VALUES (FROM_UNIXTIME(your_bigint_value));

回答by medk

ok I think this is resolved.

好的,我认为这已解决。

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-types.html

a zero value timestamp looks like this '0000-00-00 00:00:00', so it can't be used to store a unix timestamp like this : 1309347278, so it must be BIGINT.

零值时间戳看起来像这样 '0000-00-00 00:00:00',所以它不能用于存储像这样的 unix 时间戳:1309347278,所以它必须是 BIGINT。

Thanks.

谢谢。

回答by rahularyansharma

CREATE TABLE `db1`.`sms_queue` (
  `Id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `Message` VARCHAR(160) NOT NULL DEFAULT 'Unknown Message Error',
  `CurrentState` VARCHAR(10) NOT NULL DEFAULT 'None',
  `Phone` VARCHAR(14) DEFAULT NULL,  
  `LastUpdated` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
  `TriesLeft` tinyint NOT NULL DEFAULT 3,
  PRIMARY KEY (`Id`)
)
ENGINE = InnoDB;

回答by CONvid19

The correct answer is int(11), because a timestamp is a big/large integer.

正确答案是int(11),因为时间戳是一个大/大整数

回答by joakimdahlstrom

I remember I read somewhere that mySQL timestamp data type is used to be updated automatically when the row updates, is this true?

我记得我在某处读到过 mySQL 时间戳数据类型用于在行更新时自动更新,这是真的吗?

Yes.

是的。

Do I have to store timestamps with timestamp datatype or with bigint for example?

例如,我是否必须使用时间戳数据类型或 bigint 存储时间戳?

Timestamp is a datatype. You use it like latest_update TIMESTAMP(8)when you create the table.

时间戳是一种数据类型。您可以像latest_update TIMESTAMP(8)创建表一样使用它。

回答by nobody

from MySQL manual:

来自 MySQL 手册:

By default, the first TIMESTAMP column in a table is automatically set to the date and time of the most recent operation if you do not assign it a value yourself. You can also set any TIMESTAMP column to the current date and time by assigning it a NULL value.

默认情况下,表中的第一个 TIMESTAMP 列会自动设置为最近操作的日期和时间,如果您自己不为其分配值。您还可以通过为其分配 NULL 值将任何 TIMESTAMP 列设置为当前日期和时间。