V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
Emory_M
V2EX  ›  MySQL

用int类型存unix时间戳还是mysql自己的timestamp

  •  
  •   Emory_M ·
    Emory-M · Nov 24, 2011 · 14821 views
    This topic created in 5267 days ago, the information mentioned may be changed or developed.
    在设计留言板、讨论区等属于实时提交类型的数据库日期字段时
    用哪种好呢?
    8 replies    2020-09-03 14:00:38 +08:00
    sun019
        1
    sun019  
       Nov 24, 2011
    int
    Ricepig
        2
    Ricepig  
       Nov 24, 2011
    如果语言支持不好,宁愿用int
    ywjno
        3
    ywjno  
       Nov 24, 2011
    只是要日期不要时分秒毫秒的话直接char(8)
    sarices
        4
    sarices  
       Nov 25, 2011
    直接用int,方便点
    Livid
        5
    Livid  
    MOD
    PRO
       Nov 25, 2011
    并且,记得要存的是 UTC 时区的 timestamp。

    这样的话,如果有一天你的服务器的时区(比如服务器从美国挪到中国)变了,你存的时间戳不会出问题。
    Emory_M
        6
    Emory_M  
    OP
       Nov 25, 2011
    @Livid 查了下 无论是unix timestamp还是mysql的timestamp类型默认都是UTC,在取值时候进行转换
    所以不用担心,设定正确timezone就可以了

    谢谢大家回复,安心用int好了
    frittle
        7
    frittle  
       Nov 25, 2011
    @Emory_M 嗯,都是统一的。其实也不是默认,更准确的来说是unix timestamp的定义里就是使用UTC
    lizuoqiang
        8
    lizuoqiang  
       Sep 3, 2020
    @ywjno 胎神哦,那还不如用 int
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5180 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 70ms · UTC 09:41 · PVG 17:41 · LAX 02:41 · JFK 05:41
    ♥ Do have faith in what you're doing.