![]() ![]() When the column is displayed, it will be converted back for display based on whatever the current session time zone is. But once a DATETIME column has been set, it will display the same regardless of what the current session time zone is.Ī TIMESTAMP column on the other hand takes the ' 12:15:00' value you are setting into it and interprets it in the current session time zone to compute an internal representation relative to 00:00:00 UTC. The value stored will be the current date and time using the current session time zone in effect. Was this Eastern Standard Time? Pacific Standard Time? Who knows? Where the current session time zone of the server comes into play occurs when you set a DATETIME column to some value such as NOW(). So, I can set a DATETIME column to a value such as ' 12:15:00' to indicate precisely when my last birthday occurred. A DATETIME stores a literal value of a date and time with no reference to any particular timezone. I realize it may SEEM easier to use CURRENT_TIMESTAMP on an insert function.I have a slightly different perspective on the difference between a DATETIME and a TIMESTAMP. Php's date functions are outstanding to compare dates,īut in mysql or postgresql, comparing dates ? nah. (more fields in your database row is the only drawback to this solution that i have found, and that doesnt cause as many headaches, or cups of coffee :) You can just use one field, in most instances: INTEGER time_created NOT NULL or whatever you wnat to index to be searched will make for smoother and more portable databases. If you need to search, sort or compare the day from other data, or the month or the year or the day of the week, or anything, in your application,Īnd INTEGER datatype for time_day, time_hour, time_seconds. , and use gmdate() and store everything as gmt time to avoid timezone issues. ![]() it gets harder to port if you use database timestamps.Īs the number of seconds since january 1st 1970 midnight. ![]() PostgreSQL timestamp is NOT = UNIX TIMESTAMP and MySQL's UNIX TIMESTAMP is NOT PostgresQL's or Oracles timestamp. Why? because if you are developing in a middleware language like PHP, PHP has all of these functions, and they are easier to implement in the application ode as comparing integers. This might sound crazy to a lot of developers who like to take advantage of database functions,īut after exhaustive problems thinking, creating and bugfixing applications for mysql and postgrsql with php comparing date functions, I've come to the conclusion (for myself), that the easiest way, that is the simplest with less SQL headaches is not to take advantage of any of them. So the safer method is: (EXTRACT(EPOCH FROM current_timestamp) - EXTRACT(EPOCH FROM somedate))/3600Īs Michael mentioned in his follow-up comment, you'll also probably want to use floor() or round() to get the result as an integer value. But in reality 23 hours + 1 hour should be a total of 24 hours. Which converted to epoch/3600 is 25 hours. Epoch on the interval will just multiply all days by 24 hours.įor example, if a full 'short' day passes and an additional hour of the next day, the interval will be recorded as one day and one hour. The interval will take that into account, which is useful for knowing the amount of days that passed in the symbolic sense but it would give an incorrect number of the actual hours that passed. ![]() If your subtraction involves daylight savings change-overs, a particular day might be considered 23 or 25 hours respectively. Months aren't an issue in this case since subtracting two timestamps just use days and seconds but 'days' can be a problem. Intervals store things internally as months, days, and seconds. The problem is that intervals in PostgreSQL do not have context with regards to things like daylight savings. Michael Krelin's answer is close is not entirely safe, since it can be wrong in rare situations. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |