简体   繁体   English

phpmyadmin将日期int(11)导出为CSV dd / mm / yy格式

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

Im new to SQl and trying to do a dump through phpmyadmin. 我是SQl的新手,正尝试通过phpmyadmin进行转储。

At the moment date data is stored in my DB as int(11). 目前,数据以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. 当我从phpmyadmin导出时,数据自然导出为数字,例如'1325336400',但我希望将其显示为01/01/2012或类似格式。 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). 如果您以32位整数存储“日期数据”(按您的说法),那么我猜您正在使用* nix时间戳值(自1970年1月1日UTC时间以来的秒数)。

(You know this may overflow sometime in 2038, right? http://en.wikipedia.org/wiki/Year_2038_problem ) (您知道这可能会在2038年的某个时候溢出,对吧? http://en.wikipedia.org/wiki/Year_2038_problem

phpmyadmin has a hard time with these values, as you have discovered. 正如您所发现的那样,phpmyadmin在使用这些值时很难。

MySQL has a TIMESTAMP data type which also uses *nix-style timestamps. MySQL具有TIMESTAMP数据类型,该数据类型也使用* nix样式的时间戳。 (It won't overflow; the MySQL developers did the right thing.) (它不会溢出; MySQL开发人员做了正确的事。)

You really do need to convert your date data to the TIMESTAMP data type. 您确实确实需要将日期数据转换为TIMESTAMP数据类型。 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. 然后,使用已有的值填充新的ts列。

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: 接下来,更改新列的定义,以使其不允许NULL值,并使用当前时间作为默认值:

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. 最后,如果您愿意,可以摆脱带有INT值的旧列。

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 当您使用TIMESTAMP数据类型时,MySQL会尽力在内部以UTC(时区不敏感)时间存储所有这些时间戳,并根据您的设置将它们在显示时转换为本地时间

 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. 当您将它们放入数据库时​​,它还会将它们从本地时间转换为UTC时间。

Here's a write up. 这是一篇文章。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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