php 使用php在mysql表中保存时间戳

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

Saving timestamp in mysql table using php

phpmysqldatabasesql-insert

提问by gautamlakum

I have a field in a MySQL table which has a timestampdata type. I am saving data into that table. But when I pass the timestamp (1299762201428) to the record, it automatically saves the value 0000-00-00 00:00:00into that table.

我在具有timestamp数据类型的 MySQL 表中有一个字段。我正在将数据保存到该表中。但是当我将时间戳 ( 1299762201428) 传递给记录时,它会自动将值保存0000-00-00 00:00:00到该表中。

How can I store the timestamp in a MySQL table?

如何将时间戳存储在 MySQL 表中?

Here is my INSERTstatement:

这是我的INSERT声明:

INSERT INTO table_name (id,d_id,l_id,connection,s_time,upload_items_count,download_items_count,t_time,status)
VALUES (1,5,9,'2',1299762201428,5,10,20,'1'),
       (2,5,9,'2',1299762201428,5,10,20,'1')

回答by jimy

pass like this

像这样通过

date('Y-m-d H:i:s','1299762201428')

回答by Richard Tuin

Hey there, use the FROM_UNIXTIME()function for this.

您好,FROM_UNIXTIME()为此使用该功能。

Like this:

像这样:

INSERT INTO table_name
(id,d_id,l_id,connection,s_time,upload_items_count,download_items_count,t_time,status)
VALUES
(1,5,9,'2',FROM_UNIXTIME(1299762201428),5,10,20,'1'), 
(2,5,9,'2',FROM_UNIXTIME(1299762201428),5,10,20,'1')

回答by Tushar Kumar Kundan

$created_date = date("Y-m-d H:i:s");
$sql = "INSERT INTO $tbl_name(created_date)VALUES('$created_date')";
$result = mysql_query($sql);

回答by Your Common Sense

Some things to clarify:

一些事情要澄清:

  • MySQL timestamp field type doesn't store unix timestamps but rather a datetime-kind value.
  • UNIX timestamp is a number of a regular int type.
  • The timestamp you're talking about is not a regular unix timestamp but a timestamp with milliseconds.
  • MySQL 时间戳字段类型不存储 unix 时间戳,而是存储日期时间类型的值。
  • UNIX 时间戳是一些常规的 int 类型。
  • 您所说的时间戳不是常规的 unix 时间戳,而是以毫秒为单位的时间戳。

therefore the correct answer would be

因此正确答案是

$timestamp = '1299762201428';
$date = date('Y-m-d H:i:s', substr($timestamp, 0, -3));

回答by RollingBoy

Datatype 'bigint unsigned' may suit this requirement.

数据类型“ bigint unsigned”可能适合此要求。

回答by Belinda

I'm guessing that the field you are trying to save the value in is a datetime field it's not but the same seems to be true for timestamps. If so mysql expects the format to be Year-month-day Hour:minute:second. In order to save the timestamp you will have to convert the field to numeric using a query like

我猜你试图保存值的字段是一个 datetime 字段它不是但对于 timestamps 似乎也是如此。如果是这样,mysql 期望格式为年-月-日时:分:秒。为了保存时间戳,您必须使用类似的查询将字段转换为数字

alter table <table_name> change <field> <field> bigint unsigned

If you are using the current time you can use now() or current_timestamp.

如果您使用的是当前时间,则可以使用 now() 或 current_timestamp。

回答by Masood Moshref

You can use now()as well in your query, i.e. :

您也可以now()在查询中使用,即:

insert into table (time) values(now());

It will use the current timestamp.

它将使用当前时间戳。

回答by misaizdaleka

This should do it:

这应该这样做:

  $time = new DateTime; 

回答by misaizdaleka

Check field type in table just save time stamp value in datatype like bigintetc.

检查表中的字段类型只是将时间戳值保存在数据类型等中 bigint

Not datetimetype

datetime打字

回答by Udo G

Use FROM_UNIXTIME().

使用FROM_UNIXTIME().

Note: 1299762201428 looks more like a millisecond-timestamp (like Date()*1 in JavaScript), and you probably have to divide that by 1000.

注意:1299762201428 看起来更像是一个毫秒时间戳(就像 JavaScript 中的 Date()*1),你可能需要将它除以 1000。