0

Hello I need to store $_SESSION['login_time'] = time() in mysql table for which I need to create a column like "login-time" but I'm confused with these 2 datatypes "timestamp" and "datetime". Which one would be efficient in this case. Please help me...

thanks

2 Answers2

1

If you are using PHP's time() function to get the time you want to insert, it returns the time in seconds since the UNIX Epoch (Jan 1 1970 00:00:00 GMT). Since the MySQL datetime datatype supports a broader range than that ('1000-01-01 00:00:00' to '9999-12-31 23:59:59'), and the timestamp datatype supports '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC (beginning where time() begins), you might just go ahead and use timestamp since it conceptually matches up a bit better. (Unless you plan to store dates after 2038, in which case, go crazy with datetime). Either will work in your case, but the datetype vs timestamp question provides even more information about uses.

Community
  • 1
  • 1
jcmeloni
  • 1,259
  • 1
  • 15
  • 21
0

Clearely, here, timestamp is the best choice. When such a question comes up, the only way ton answer it is "what is the meaning of what I store". the login time is a "point in time" that you will update many times. So between the two types you are facing, you will chose the one that means "point in time" and not "date of the calendar and time of the clock". DateTime is here to store everything that is dated. TimeStamp is just here to mark the time.

Login time by definition a timestamp, so using Timstamp type must be wiser and more efficient.

The only limit of this answer is that timestamp is technically limited to 2038.

artragis
  • 3,677
  • 1
  • 18
  • 30
  • 1
    You might want to mention the "automatic update" behavior of a `TIMESTAMP` (which can be overridden) but often catches the uninitiated by surprise. – spencer7593 Mar 16 '13 at 14:56