Saving timestamp in mysql table using php

2019-03-07 20:08发布

I have a field in a MySQL table which has a timestamp data 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:00 into that table.

How can I store the timestamp in a MySQL table?

Here is my INSERT statement:

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')

15条回答
smile是对你的礼貌
2楼-- · 2019-03-07 20:45

pass like this

date('Y-m-d H:i:s','1299762201428')
查看更多
Deceive 欺骗
3楼-- · 2019-03-07 20:54

Use datetime field type. It comes with many advantages like human readability (nobody reads timestamps) and MySQL functions.

To convert from a unix timestamp, you can use MySQL function FROM_UNIXTIME(1299762201428). To convert back you can use UNIX_TIMESTAMP: SELECT UNIX_TIMESTAMP(t_time) FROM table_name.

Of course, if you don't like MySQL function, you could always use PHP: 'INSERT INTO table_name SET t_time = ' . date('Y-m-d H:i:s', $unix_timestamp).

查看更多
Fickle 薄情
4楼-- · 2019-03-07 20:55

Use FROM_UNIXTIME().

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

查看更多
Melony?
5楼-- · 2019-03-07 20:57

Datatype 'bigint unsigned' may suit this requirement.

查看更多
老娘就宠你
6楼-- · 2019-03-07 20:57

If the timestamp is the current time, you could use the mysql NOW() function

查看更多
不美不萌又怎样
7楼-- · 2019-03-07 20:57

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

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

It will use the current timestamp.

查看更多
登录 后发表回答