Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Range:</strong></p> <p>There's always the obvious disadvantage: The range that you can store is limited från 1970 to 2038. If you need to store dates outside of this range, you'll generally need to use another format. The most common case I've found where this apply is to birthdates.</p> <p><strong>Readability:</strong></p> <p>I think that the most important reason that people chose to use one of the built-in date-types it that the data is easier to interpret. You can do a simple select, and understand the values without having to format the response further.</p> <p><strong>Indexes:</strong></p> <p>A good technical reason to use the date types is that it allows for indexed query in some cases that unix timestamps doesn't. Consider the following query:</p> <pre><code>SELECT * FROM tbl WHERE year(mydate_field) = 2009; </code></pre> <p>If mydate_field is of a native date type, and there's an index on the field, this query will actually use an index, despite the function call. This is pretty much the only time that mysql can optimize function calls on fields like this. The corresponding query on a timestamp field won't be able to use indices:</p> <pre><code>SELECT * FROM tbl WHERE year(from_unixtime(mytimestamp_field)) = 2009; </code></pre> <p>If you think about it for a bit, there's a way around it, though. This query does the same thing, and <em>will</em> be able to use index optimizations:</p> <pre><code>SELECT * FROM tbl WHERE mytimestamp_field &gt; unix_timestamp("2009-01-01") AND mytimestamp_field &lt; unix_timestamp("2010-01-01"); </code></pre> <p><strong>Calculations:</strong></p> <p>Generally, I store dates as unix time, despite the disadvantages. This isn't really based on it's merits, but rather it's because I'm used to it. I've found that this simplifies some calculations, but complicate others. For example, it's very hard to add a month to a unix timestamp since the number of seconds per month varies. This is very easy using the mysql DATE_ADD() function. However, I think that in most cases it actually simplifies calculations. For example, it's quite common that you want to select the posts from, say, the last two days. If the field contains a unix timestamp this can be done easily by simply doing:</p> <pre><code>SELECT * FROM tbl WHERE mytimestamp_field &gt; time() - 2*24*3600; </code></pre> <p>It's probably a matter of taste, but I personally find this faster and easier than having to rember the syntax of a function such as DATE_SUB().</p> <p><strong>Timezones:</strong></p> <p>Unix timestamps can't store time zone data. I live in sweden which has a single timezone, so this isn't really a problem for me. However, it can be a major pain if you live in a country that spans multiple timezones.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload