Are unix timestamps the best way to store timestamps?

后端 未结 9 859
梦毁少年i
梦毁少年i 2020-12-07 16:26

I always use unix timestamps for everything, but am wondering if there is a better way.

What do you use to store timestamps and why?

相关标签:
9条回答
  • 2020-12-07 16:58

    UNIX Timestamp 32-bit problem seems to be pretty annoying for users who enter future dates in 2038+.

    Either use the DATETIME sequence for MySQL, or store your dates as BIGINT(8) unsigned (max: 18 quintillion) or FLOAT so that you can enter large numbers. Then you cannot use for example PHP's date() function because it only allows integers as parameter (limited by 32-bit systems).

    The solution I found is to use PHP 5.2.0 functions. Here's the DateTime PHP solution.

    No need to change UNIX_TIMESTAMP format. As long as you have BIGINT(8) unsigned as your MySQL storage for timestamps. You won't be limited by 32-bit systems anymore.

    0 讨论(0)
  • 2020-12-07 17:03

    A timestamp is bascially:

    • a distinct point in time

    And as a point in time has an endless resolution, the important thing on choosing a timestamp format is: has it enough resolution?

    • Unix time counts only in seconds.
    • Ext 4 has nanoseconds
    • Java has nanoseconds

    For most applications I had, nanoseconds were enough. So Java Timestamp had the right resolution for me so far.

    0 讨论(0)
  • 2020-12-07 17:09

    32 bit Unix timestamps will overflow in a few years (January 2038), so that might be a consideration. I generally use a DATETIME format in SQL, which is YYYY-MM-DD HH:MM:SS with the time as a 24-hour clock. I try to output to files in the same format, just to make my life easier.

    0 讨论(0)
提交回复
热议问题