I have a column in my database called time
. the type of this column is timestamp
and Default value is CURRENT_TIMESTAMP
But after some inserts, in phpMyAdmin
it shows the value as datetime, e.g. 2019-05-05 04:24:45
and even the Timezone is shown there and can be changed!
I thought MySQL’s timestamp is 4 bytes (compared to 8 bytes of datetime) and doesn’t store timezone and data is same as INT(10) such as: 1557094115
(seconds passed since 1970 or something like that)
Can any one please explain this, is it a bug or something?
MySQL version 5.7.25
Edit 1 (Screenshots):
It is a TIMESTAMP
column, with default value of CURRENT_TIMESTAMP
As you see it is shown as DATETIME
and I cannot compare it with integer value of unix_timestamp… also we can change TimeZone to any value (I thought timestamp doesn’t store timezone…)
Edit 2:
If (based on one answer) MySQL stores it as an integer internally, then why can’t I compare it with integers? (the following query won’t work)
DELETE FROM `table` WHERE time < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL :days DAY))
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: ‘1555980012’ for column ‘time’ at row 1
I also tried it in Sequel Pro and MySQLWorkbench with same results
3
Answers
If you need to see
1557094115
, then apply the functionUNIX_TIMESTAMP()
to theTIMESTAMP
orDATETIME
column. It’s inverse isFROM_UNIXTIME()
.More
The internal storage for
TIMESTAMP
is 1557055485 in UTC; the timezone is added/removed as it is fetched/stored.The internal storage for
DATETIME
is (logically, but not actually) the string “2019-05-05 04:24:45” with no hint of timezone. (Actually, it is packed into 5 bytes in some fashion.)Without any conversion function, fetching
TIMESTAMP
andDATETIME
look the same:Adding
NOW()
to both, thenSELECTing
:Since MySQL 5.6.4, the DATETIME field requires 5 bytes + 3 bytes fractional. The TIMESTAMP type requires 4 bytes + 3 bytes fractional. Neither of these data types store time zone information. However, both MySQL and phpMyAdmin display TIMESTAMP fields according to the timezone of the database server. You can retrieve the database server’s timezone info with the following statements:
If you would like phpMyAdmin to display a different timezone from the database server, you can set the SessionTimeZone property inside of phpMyAdmin’s config.inc.php file.