<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Birthday MySQL Query Hates Timestamps</title>
	<atom:link href="http://blog.ninedays.org/2008/02/20/birthday-mysql-query-hates-timestamps/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.ninedays.org/2008/02/20/birthday-mysql-query-hates-timestamps/</link>
	<description>web development, photography, tutorials and adventure</description>
	<pubDate>Tue, 06 Jan 2009 05:32:13 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.5</generator>
		<item>
		<title>By: Terri Ann</title>
		<link>http://blog.ninedays.org/2008/02/20/birthday-mysql-query-hates-timestamps/#comment-810</link>
		<dc:creator>Terri Ann</dc:creator>
		<pubDate>Mon, 30 Jun 2008 14:14:44 +0000</pubDate>
		<guid isPermaLink="false">http://blog.ninedays.org/2008/02/20/birthday-mysql-query-hates-timestamps/#comment-810</guid>
		<description>&lt;p&gt;To select next/last week's birthdays you'll have to use the &lt;code&gt;DATE_ADD()&lt;/code&gt; and &lt;code&gt;DATE_SUB()&lt;/code&gt; to subtract &lt;code&gt;INTERVAL&lt;/code&gt;s from the current date.  We'll select form the range between &lt;code&gt;NOW()&lt;/code&gt; and &lt;code&gt;DATE_ADD(NOW(), INTERVAL 7 DAY)&lt;/code&gt; which is 7 days (1 week) from now.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# next week's birthdays (inclusive)
SELECT * FROM `users` WHERE
 DATE_FORMAT(`birthdate`, '%m%d') &#62;= DATE_FORMAT(NOW(), '%m%d') AND
 DATE_FORMAT(`birthdate`, '%m%d') &#60;= DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 7 DAY), '%m%d')
 ORDER BY DATE_FORMAT(`birthdate`, '%m%d') ASC;
 # 28 total, Query took 0.0309 sec - 1000 row table
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Don't forget that if you want to sort the birthdays by birthday (aka day) you'll need to eliminate the year from the sort order or MySQL wll order things using the year too.  That's why my &lt;code&gt;ORDER&lt;/code&gt; in these sample queries are &lt;code&gt;ORDER BY DATE_FORMAT(&lt;/code&gt;birthdate&lt;code&gt;, '%m%d') ASC&lt;/code&gt;.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# last week's birthdays (inclusive)
SELECT * FROM `users` WHERE
 DATE_FORMAT(`birthdate`, '%m%d') &#60;= DATE_FORMAT(NOW(), '%m%d') AND
 DATE_FORMAT(`birthdate`, '%m%d') &#62;= DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 7 DAY), '%m%d')
 ORDER BY DATE_FORMAT(`birthdate`, '%m%d') ASC;
 # 20 total, Query took 0.0469 sec - 1000 row table
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;If anyone knows of a more efficent way to set this up, let me know.  I haven't run any extensive benchmarks and don't intend on it any time soon.  If you do, send your responses this way! &lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>To select next/last week's birthdays you'll have to use the <code>DATE_ADD()</code> and <code>DATE_SUB()</code> to subtract <code>INTERVAL</code>s from the current date.  We'll select form the range between <code>NOW()</code> and <code>DATE_ADD(NOW(), INTERVAL 7 DAY)</code> which is 7 days (1 week) from now.</p>

<pre><code># next week's birthdays (inclusive)
SELECT * FROM `users` WHERE
 DATE_FORMAT(`birthdate`, '%m%d') &gt;= DATE_FORMAT(NOW(), '%m%d') AND
 DATE_FORMAT(`birthdate`, '%m%d') &lt;= DATE_FORMAT(DATE_ADD(NOW(), INTERVAL 7 DAY), '%m%d')
 ORDER BY DATE_FORMAT(`birthdate`, '%m%d') ASC;
 # 28 total, Query took 0.0309 sec - 1000 row table
</code></pre>

<p>Don't forget that if you want to sort the birthdays by birthday (aka day) you'll need to eliminate the year from the sort order or MySQL wll order things using the year too.  That's why my <code>ORDER</code> in these sample queries are <code>ORDER BY DATE_FORMAT(</code>birthdate<code>, '%m%d') ASC</code>.</p>

<pre><code># last week's birthdays (inclusive)
SELECT * FROM `users` WHERE
 DATE_FORMAT(`birthdate`, '%m%d') &lt;= DATE_FORMAT(NOW(), '%m%d') AND
 DATE_FORMAT(`birthdate`, '%m%d') &gt;= DATE_FORMAT(DATE_SUB(NOW(), INTERVAL 7 DAY), '%m%d')
 ORDER BY DATE_FORMAT(`birthdate`, '%m%d') ASC;
 # 20 total, Query took 0.0469 sec - 1000 row table
</code></pre>

<p>If anyone knows of a more efficent way to set this up, let me know.  I haven't run any extensive benchmarks and don't intend on it any time soon.  If you do, send your responses this way! </p>]]></content:encoded>
	</item>
	<item>
		<title>By: Terri Ann</title>
		<link>http://blog.ninedays.org/2008/02/20/birthday-mysql-query-hates-timestamps/#comment-808</link>
		<dc:creator>Terri Ann</dc:creator>
		<pubDate>Mon, 30 Jun 2008 13:31:51 +0000</pubDate>
		<guid isPermaLink="false">http://blog.ninedays.org/2008/02/20/birthday-mysql-query-hates-timestamps/#comment-808</guid>
		<description>&lt;p&gt;@mick - You learn something new every day, thanks.  I haven't seen the &lt;code&gt;from_unixtime()&lt;/code&gt; function before.  Even though it doesn't work before 1/1/1970 it's useful to add to the repository!&lt;/p&gt;

&lt;p&gt;@mark - Oh, I know this one, give me a little bit to find this snippet somewhere in my library and I'll post it right up!&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>@mick - You learn something new every day, thanks.  I haven't seen the <code>from_unixtime()</code> function before.  Even though it doesn't work before 1/1/1970 it's useful to add to the repository!</p>

<p>@mark - Oh, I know this one, give me a little bit to find this snippet somewhere in my library and I'll post it right up!</p>]]></content:encoded>
	</item>
	<item>
		<title>By: mick</title>
		<link>http://blog.ninedays.org/2008/02/20/birthday-mysql-query-hates-timestamps/#comment-687</link>
		<dc:creator>mick</dc:creator>
		<pubDate>Sun, 29 Jun 2008 21:10:54 +0000</pubDate>
		<guid isPermaLink="false">http://blog.ninedays.org/2008/02/20/birthday-mysql-query-hates-timestamps/#comment-687</guid>
		<description>&lt;p&gt;Ignore my last comment :) It doesn't work for dates before jan 1 1970 since they're not supported by from_unixtime&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Ignore my last comment :) It doesn't work for dates before jan 1 1970 since they're not supported by from_unixtime</p>]]></content:encoded>
	</item>
	<item>
		<title>By: mick</title>
		<link>http://blog.ninedays.org/2008/02/20/birthday-mysql-query-hates-timestamps/#comment-686</link>
		<dc:creator>mick</dc:creator>
		<pubDate>Sun, 29 Jun 2008 21:01:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.ninedays.org/2008/02/20/birthday-mysql-query-hates-timestamps/#comment-686</guid>
		<description>&lt;p&gt;I know this post is a little old but just wanted to post this...&lt;/p&gt;

&lt;p&gt;"Unfortunately there is no query to convert and update all 1000 users birthday (datetime data type) with the data from dob (Unix time stamp stored as INT)"&lt;/p&gt;

&lt;p&gt;How about this?&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;UPDATE users SET birthdate=from_unixtime(dob);
&lt;/code&gt;&lt;/pre&gt;
</description>
		<content:encoded><![CDATA[<p>I know this post is a little old but just wanted to post this...</p>

<p>"Unfortunately there is no query to convert and update all 1000 users birthday (datetime data type) with the data from dob (Unix time stamp stored as INT)"</p>

<p>How about this?</p>

<pre><code>UPDATE users SET birthdate=from_unixtime(dob);
</code></pre>]]></content:encoded>
	</item>
	<item>
		<title>By: Mark</title>
		<link>http://blog.ninedays.org/2008/02/20/birthday-mysql-query-hates-timestamps/#comment-484</link>
		<dc:creator>Mark</dc:creator>
		<pubDate>Mon, 12 May 2008 17:31:20 +0000</pubDate>
		<guid isPermaLink="false">http://blog.ninedays.org/2008/02/20/birthday-mysql-query-hates-timestamps/#comment-484</guid>
		<description>&lt;p&gt;This is close to what I am looking for.&lt;/p&gt;

&lt;p&gt;How would I get all the birthdays for tomorrow or all the birthdays for the next week/month?&lt;/p&gt;

&lt;p&gt;Would I have to use PHP to set the time range or can i do it with one quick query????&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>This is close to what I am looking for.</p>

<p>How would I get all the birthdays for tomorrow or all the birthdays for the next week/month?</p>

<p>Would I have to use PHP to set the time range or can i do it with one quick query????</p>]]></content:encoded>
	</item>
</channel>
</rss>
