简体   繁体   中英

phpmyadmin export of date int(11) to CSV dd/mm/yy format

Im new to SQl and trying to do a dump through phpmyadmin.

At the moment date data is stored in my DB as int(11).

When I export from phpmyadmin, the data is naturally exported as numbers like '1325336400' but i would like this to display as 01/01/2012 or similar format. is there any way I can do this?

Many thanks in advance

Jus

If you're storing your "date data" (as you put it) in 32-bit integers, I guess you are using *nix timestamp values (seconds since the 1-jan-1970 00:00 UTC epoch).

(You know this may overflow sometime in 2038, right? http://en.wikipedia.org/wiki/Year_2038_problem )

phpmyadmin has a hard time with these values, as you have discovered.

MySQL has a TIMESTAMP data type which also uses *nix-style timestamps. (It won't overflow; the MySQL developers did the right thing.)

You really do need to convert your date data to the TIMESTAMP data type. Otherwise dealing with time will be a huge pain in the neck, forever. Here's how to do it.

First, add a column to your table in this way,

ALTER TABLE mytable ADD COLUMN ts TIMESTAMP AFTER myinttimestamp

Then populate your new ts column using the values you already have.

UPDATE TABLE mytable SET ts = FROM_UNIXTIME(myinttimestamp)

Next, change the definition of your new column so it disallows NULL values and uses the current time as a default:

ALTER TABLE mytable 
     CHANGE ts
            ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL

Finally, if you want you can get rid of the old column with the INT values in it.

ALTER TABLE mytable DROP COLUMN myinttimestamp

(You should consider trying all this out on a copy of your table; it would stink to make a mistake and wreck your data).

When you use the TIMESTAMP data type, MySQL does its best to store all these timestamps internally in UTC (time-zone-insensitive) time, and convert them to local time upon display, based on how you set

 SET time_zone = 'Asia/Vladivostok' 

or whatever. It will also convert them from local time to UTC time when you put them in to the data base.

Here's a write up.

https://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM