timestamp, datetime and unix timestamp innocently tell?

Copyright: If you please indicate the source, the exchange of learning: [email protected] https://blog.csdn.net/weter_drop/article/details/89917187

Write a program to build a database can not avoid using the date and time for the database, there are a variety of date and time fields to choose from, such as timestamp and datetime and use int to store UNIX timestamp ((UNIX timestamp) UNIX timestamp from 1970 May 1 (UTC / GMT midnight) the number of seconds to start now elapsed).

Often someone with a string data type to store date (not the right way)

One disadvantage: comparison can not be calculated and date functions

2 shortcomings: string stored date to take up more space

Not only novices, including some experienced programmers still relatively confused, exactly what type should I use to store the date and time it?

Then we go step by step analysis of their characteristics, so that we select the appropriate field type according to their own needs to store (compare the advantages and disadvantages are out, parents with small children like to take with their neighbors than the same)

datetime 和 timestamp

  • datetime more like a combination of the above time period of the calendar and you watch, refers to a specific time.
  • timestamp is more suitable to record the time, like I was in the East eight districts time now is 2016-08-02 10:35:52, you are in Japan (East At this time nine districts 2016-08-02 11:35:52), I'm with you in the chat, database records the time, then taken out, for me, time is 2016-08-02 10:35:52, for you, is Japan 2016-08-02 11:35:52. So the calculation time zones would not have considered.
  • Time timestamp is flawed (1970-2038), of course datetime (1000-9999) can not record when Liu Bei was born (161 years).

timestamp 和 UNIX timestamp

  • timestamp visual display, to facilitate troubleshooting a problem, looking much better than the figures of the long int
  • UNIX timestamp is accumulated since 1970, but is supported by int range 1901-12-13 to 2038-01-19 03:14:07, if you need a larger area needs to be set bigInt. But this time is not included in milliseconds, ms if desired, also need to define a floating point number. datetime and timestamp native carrying six microsecond.
  • timestamp, the same item 2 above own time zone conversion.
  • Time usually distal date type of user input, int front if the storage memory required acquisition process

to sum up

  • timestamp recording changes frequently update / create / publish / Log Time / Buy Time / Time Login / registration time, etc., and is the recent time, enough, time zone, automatic processing, such as shopping or doing overseas business could expand to overseas
  • datetime record a fixed time, such as a server to perform the scheduled task time / fitness and exercise program time in any time zone is the need for a fixed period of time to do a certain thing. Beyond the timestamp of the time, if the time zone needs have to remember that the time zone deal
  • UNIX timestamps is not very convenient to use, As for the scope of comparison to take what, timestamp, and datetime are capable.
  • If you do not consider time zone, or time zone has its own set of programs, random, and the like which which
  • laravel international framework is designed for the convenience of the programmer, in line with database design standards, so created_at updated_at use the timestamp is understandable.
  • Is there a time range type that is resolved, the problem of time zone? It is impossible, is not there tinyInt BigInt it? We need to take, and the MySQL database that allows field changes.
  • Birthday may be used to store a plurality of fields, such as year / month / day, so that you can easily find the user's birthday day (User :: where ([ 'month' => 8, 'day' => 12] ) -> get ())

When building your projects need to seriously think about their own business scenarios exactly in what is more suitable. Which to choose? Needs to be.

Guess you like

Origin blog.csdn.net/weter_drop/article/details/89917187