Question

I am in need of a MySQL column that can store a specific date in time. The application I am working with gives me a UNIX timestamp (seconds that have elapsed since January 1, 1970). I need to store this information in the database and retrieve it later in specific ways, see below.

After this date has been stored I need to be able to query the database based on this timestamp. For example, I need to be able to retrieve all rows where the day of the timestamp is the 4th.

So then my question is how do I store this date where it would be easiest to perform the query? DATETIME and TIMESTAMP seem like valid options for me but I can't seem to figure out how to work the query. I am not looking forward to storing the timestamp as an integer as it wouldn't allow me to create such a query and I'd have to offload that work to PHP.

If it helps I need to deal with these timestamps in UTC time. I know TIMESTAMP will do the conversion into PST (my local time) for me and so I'd have to do the conversion back to UTC either in MySQL or PHP. (at least I think I would have to)

Please help!

Was it helpful?

Solution

If I were you, I'll save those data's as DateTime. The reasons are

  • the records are readable with our own eye. eg 2013-01-04 00:25:12 is better than 1357259112.
  • you don't have to use any conversion function like FROM_UNIXTIME to which i think kills the index. So it is much better to use date or datetime because you can utilize index since you can search the value directly by using = or BETWEEN

OTHER TIPS

Store it as an integer, and then use MySQL's FROM_UNIXTIME() function as follows:

SELECT
    `field_list`
FROM
    `table_name`
WHERE
    FROM_UNIXTIME(`the_date`, '%d') = 4;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top