the type is integer like :
int(11)
is good for indexing and conditions like > < =
Correct Column Type For a Unix Timestamp with Microseconds
database design - Store date as unix timestamp or TIMESTAMP data type in MySQL? - Stack Overflow
php - How to store UNIX timestamps with MySQL - Stack Overflow
sql - Is it possible to create a column with a UNIX_TIMESTAMP default in MySQL? - Stack Overflow
Unix time_t is either 32 bits wide, or 64. So, int(8) or binary(8) is sufficient, at least for the next 293 billion years.
The number in a MySQL INT(n) datatype doesn't specify how much storage space is reserved, it's a display-width for formatting purposes only. As such an INT(10) is the same as a plain INTEGER, that is to say a 32-bit signed number.
So this is certainly an appropriate datatype for a 32-bit Unix timestamp. But if you want 64-bit timestamps it's not going to be enough; you would have to use a BIGINT.
Hi,
Currently I have a column of type char that stores unix time with microseconds as follows:
`last_update_utime` char(18) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'Unix Time of Last Update with mseconds'
I update that column as below;
UPDATE `transaction_tbl`
SET `transaction_token` = '$token',
`last_update_utime` = UNIX_TIMESTAMP(LOCALTIMESTAMP(6))
WHERE `user_id` = '$userID'
I use the field last_update_utime to address concurrency.
What is the correct column type for unix timestamps with microseconds ?
i have seen posts on the web stating int(11) or bigint(20)
but the issue of microseconds is not addressed ?
Is keeping the column type to 'char' sufficient or better to use something like decimal(18,6) ?
Pls advise.
Usually it does not matter whether you use TIMESTAMP or DATETIME datatype.
- In older versions,
TIMESTAMPwas 4 bytes andDATETIMEwas 8. - Think of
DATETIMEas a picture of a clock; think ofTIMESTAMPas an instant in time, worldwide. That is, if you connect to the same database, but from a different timezone, aDATETIMEwill look the the same, but aTIMESTAMPwill be adjusted for timezone. NOW(),SELECTinginto PHP, etc, are compatible with both.- Both are externally seen as a string, such as '2015-04-25 17:09:01'.
- Since
TIMESTAMPis stored as a 32-bit integer (but you don't see that), it is limited to ~1970-2038. - Since
DATETIMEis clock time, there will be a missing/extra hour twice a year if you switch to/from daylight savings time.
Yes, you could use UNIX_TIMESTAMP() and have an INT UNSIGNED, but wouldn't it be better to see '2015-...'? (That would be 4 bytes.)
You can use DateTime type to store timestamp & insert current timestamp using now() in mysql or get dates/timestamps via any methods you find suitable.
A Unix timestamp is a large integer (the number of seconds since 1970-01-01 00:00:00 UTC), so INT(11) is the correct datatype.
Unfortunately, I don't think there's any way to specify a default that will insert the current timestamp. You'll need to call UNIX_TIMESTAMP() explicitly when inserting, and use that. Function calls aren't allowed in DEFAULT specifications.
Actually, you have to use either bigint or varchar because the maximum for int(11) is 2'147'483'647 (more info here).
Then, as the previous answers say, you have to manually insert UNIX_TIMESTAMP()
The way MySQL implements the TIMESTAMP data type, it is actually storing the epoch time in the database. So you could just use a TIMESTAMP column with a default of CURRENT_TIMESTAMP and apply the UNIX_TIMESTAMP() to it if you want to display it as an int:
CREATE TABLE foo(
created TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);
insert into foo values (current_Date()),(now());
select unix_timestamp(created) from foo;
+-------------------------+
| unix_timestamp(created) |
+-------------------------+
| 1300248000 |
| 1300306959 |
+-------------------------+
2 rows in set (0.00 sec)
However, if you really want the datatype of the column to be INT, you can use R. Bemrose's suggestion and set it via trigger:
CREATE TABLE foo(
created INT NULL
);
delimiter $$
create trigger tr_b_ins_foo before insert on foo for each row
begin
if (new.created is null)
then
set new.created = unix_timestamp();
end if;
end $$
delimiter ;
insert into foo values (unix_timestamp(current_Date())), (null);
select created from foo;
+------------+
| created |
+------------+
| 1300248000 |
| 1300306995 |
+------------+
2 rows in set (0.00 sec)
From the documentation:
With one exception, the default value must be a constant; it cannot be a function or an expression. This means, for example, that you cannot set the default for a date column to be the value of a function such as NOW() or CURRENT_DATE. The exception is that you can specify CURRENT_TIMESTAMP as the default for a TIMESTAMP column.
Timestamps in MySQL are generally used to track changes to records, and are often updated every time the record is changed. If you want to store a specific value you should use a datetime field.
If you meant that you want to decide between using a UNIX timestamp or a native MySQL datetime field, go with the native DATETIME format. You can do calculations within MySQL that way
("SELECT DATE_ADD(my_datetime, INTERVAL 1 DAY)") and it is simple to change the format of the value to a UNIX timestamp ("SELECT UNIX_TIMESTAMP(my_datetime)") when you query the record if you want to operate on it with PHP.
Also, as of MySQL 8.0.19 the DATETIME supports time zone offsets, so there's even less reason to use TIMESTAMP now.
In MySQL 5 and above, TIMESTAMP values are converted from the current time zone to UTC for storage, and converted back from UTC to the current time zone for retrieval. (This occurs only for the TIMESTAMP data type, and not for other types such as DATETIME.)
By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis, as described in MySQL Server Time Zone Support.