Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 692 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Should I use the datetime or timestamp data type in MySQL?

#1
Would you recommend using a [datetime][1] or a [timestamp][1] field, and why (using MySQL)?

I'm working with PHP on the server side.


[1]:

[To see links please register here]

Reply

#2
I prefer using timestamp so to keep everything in one common raw format and format the data in PHP code or in your SQL query. There are instances where it comes in handy in your code to keep everything in plain seconds.
Reply

#3
TIMESTAMP is 4 bytes Vs 8 bytes for DATETIME.

[

[To see links please register here]

][1]

But like scronide said it does have a lower limit of the year 1970. It's great for anything that might happen in the future though ;)


[1]:

[To see links please register here]

Reply

#4
Depends on application, really.

Consider setting a timestamp by a user to a server in New York, for an appointment in Sanghai. Now when the user connects in Sanghai, he accesses the same appointment timestamp from a mirrored server in Tokyo. He will see the appointment in Tokyo time, offset from the original New York time.

So for values that represent user time like an appointment or a schedule, datetime is better. It allows the user to control the exact date and time desired, regardless of the server settings. The set time is the set time, not affected by the server's time zone, the user's time zone, or by changes in the way daylight savings time is calculated (yes it does change).

On the other hand, for values that represent system time like payment transactions, table modifications or logging, always use timestamps. The system will not be affected by moving the server to another time zone, or when comparing between servers in different timezones.

Timestamps are also lighter on the database and indexed faster.
Reply

#5
I make this decision on a semantic base.

I use a timestamp when I need to record a (more or less) fixed point in time. For example when a record was inserted into the database or when some user action took place.

I use a datetime field when the date/time can be set and changed arbitrarily. For example when a user can save later change appointments.
Reply

#6
I always use DATETIME fields for anything other than row metadata (date created or modified).

As [mentioned][1] in the MySQL documentation:

> The DATETIME type is used when you need values that contain both date and time information. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
>
> ...
>
> The TIMESTAMP data type has a range of '1970-01-01 00:00:01' UTC to '2038-01-09 03:14:07' UTC. It has varying properties, depending on the MySQL version and the SQL mode the server is running in.

You're quite likely to hit the lower limit on TIMESTAMPs in general use -- e.g. storing birthdate.


[1]:

[To see links please register here]

Reply

#7
The main difference is that DATETIME is constant while TIMESTAMP is affected by the `time_zone` setting.

So it only matters when you have — or may in the future have — synchronized clusters across time zones.

In simpler words: **If I have a database in Australia, and take a dump of that database to synchronize/populate a database in America, then the TIMESTAMP would update to reflect the real time of the event in the new time zone, while DATETIME would still reflect the time of the event in the au time zone**.

A great example of DATETIME being used where TIMESTAMP should have been used is in Facebook, where their servers are never quite sure what time stuff happened across time zones. Once I was having a conversation in which the time said I was replying to messages before the message was actually sent. (This, of course, could also have been caused by bad time zone translation in the messaging software if the times were being posted rather than synchronized.)
Reply

#8
The major difference is

- a INDEX's on Timestamp - works
- a INDEX's on Datetime - [Does not work][1]

look at this [post to see problems with Datetime indexing][2]


[1]:

[To see links please register here]

[2]:

[To see links please register here]

Reply

#9
Another difference between Timestamp and Datetime is in Timestamp you can't default value to NULL.
Reply

#10
In MySQL 5 and above, **TIMESTAMP** values are converted from the current time zone to UTC for storage, and converted back from UTC to the current time zone for retrieval. (This occurs only for the TIMESTAMP data type, and *not* for other types such as DATETIME.)

By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis, as described in *[MySQL Server Time Zone Support][1]*.

[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through