106

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')
2
  • 2
    What timestamp you're talking about? Where did you get it? Commented Apr 12, 2011 at 9:05
  • 1
    can u paste ur table structure here ? Commented Apr 12, 2011 at 9:06

15 Answers 15

180

pass like this

date('Y-m-d H:i:s','1299762201428')
Sign up to request clarification or add additional context in comments.

6 Comments

but the value you get in db shows the column type is datetime, if you want to change it to timestamp then change the column type to varchar(15)
Then put it on an INT field. Anyway, a timestamp is only a representation of a date, and vice versa. You can convert from timestamp to date with the function jimy told you, and the other way with strtotime. edit: btw, timestamp only covers a range of all possible dates (1970-01-01 to xx-xx-2032 I think)
so what is the use of timestamp datatype? s_time field has timestamp datatype. Can't I save 1299762201428 to that field?
@lakum4stackof: For timestamp datatype, please refer to dev.mysql.com/doc/refman/5.0/en/timestamp.html for detail.
@lakum4stackof - the use of timestamp is that you do save the timestamp, but it is only displayed as a date. Internally (as all data types) it's stored as signed integer. If you also want to format that timestamp as an integer, I suggest that you just use INT field then. Use of timestamp column is the date manpipulation (adding intervals and such).
|
57

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

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

3 Comments

Why is this better than date('Y-m-d H:i:s','1299762201428')?
Type safety: FROM_UNIXTIME yields a native mysql date type while php's date() returns a string.
It's better because web server ( PHP ) and MySQL could be in different places. So, date('Y-m-d H:i:s','1299762201428') will set the TimeZone of the web server. With different web servers placed in different zones you will have inconsistent data. Alternatively, in your code, you have to force the application timezone to the timezone of MySQL. date_timezone_set
20
$created_date = date("Y-m-d H:i:s");
$sql = "INSERT INTO $tbl_name(created_date)VALUES('$created_date')";
$result = mysql_query($sql);

Comments

16

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.

therefore the correct answer would be

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

Comments

5

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

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

It will use the current timestamp.

Comments

3

Datatype 'bigint unsigned' may suit this requirement.

Comments

2

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

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

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

Comments

1

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.

Comments

1

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

Not datetime type

Comments

1

This should do it:

  $time = new DateTime; 

Comments

0

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

Comments

0

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

3 Comments

The timestamp field type is human readable too (when using SELECT in the console). They are very different and both have their dis/advantages. The main difference is how time zones are being handled.
@Udo G: You are correct, but I still prefer datetime for various reasons. What is the biggest dis/advantage in your opinion?
biggest advantage for timestamp: it better matches the PHP timestamps as it is immune to time zone settings of both the server and the client, whereas datetime changes it's displayed value depending on the time zone of your MySQL client (of course it depends on your project, which one is better). biggest disadvantage for timestamp: it does not support NULL values and (I don't know what they did smoke when they programmed this) a TIMESTAMP NOT NULL field becomes a TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP. In my case I unfortunately need to use TIMESTAMP for TZ reasons.
0

Better is use datatype varchar(15).

3 Comments

Saving a number in the form of string is usually not a good pratice, for strings are slower in comparison and harder to perform calculation.
My vote goes to unsigned int-- but let's go down the rabbit hole. Why varchar and not char? It is a fixed length entry-- micro-optimization, but still...
timestamp is int, you should not use varchar for this kind of value
0

You can do: $date = \gmdate(\DATE_ISO8601);.

Comments

-1

If I know the database is MySQL, I'll use the NOW() function like this:

INSERT INTO table_name
   (id, name, created_at) 
VALUES 
   (1, 'Gordon', NOW()) 

1 Comment

This would store the current timestamp only, not strictly what OP is asking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.