<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Bill Richards &#187; usort()</title>
	<atom:link href="http://www.wrichards.com/blog/tag/usort/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wrichards.com/blog</link>
	<description>a developer&#039;s blog</description>
	<lastBuildDate>Tue, 20 Jul 2010 16:32:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Getting Upcoming Birthdays From MySQL</title>
		<link>http://www.wrichards.com/blog/2009/03/getting-upcoming-birthdays-from-mysql/</link>
		<comments>http://www.wrichards.com/blog/2009/03/getting-upcoming-birthdays-from-mysql/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 23:51:52 +0000</pubDate>
		<dc:creator>Bill</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[birthday]]></category>
		<category><![CDATA[usort()]]></category>

		<guid isPermaLink="false">http://www.wrichards.com/blog/?p=85</guid>
		<description><![CDATA[Assuming you have a table called `users` with birthday as `bday` for the column name, this will pull all users who are having a birthday in the next 7 days. This is very useful if you want to showcase your member&#8217;s birthdays on your website. SELECT * FROM `users` WHERE DAYOFYEAR(curdate()) = dayofyear(`bday`) LIMIT 30; [...]]]></description>
			<content:encoded><![CDATA[<p>Assuming you have a table called `users` with birthday as `bday` for the column name, this will pull all users who are having a birthday in the next 7 days. This is very useful if you want to showcase your member&#8217;s birthdays on your website.</p>
<pre name="code" class="php">
SELECT * FROM `users` WHERE  DAYOFYEAR(curdate()) <= dayofyear(`bday`) AND DAYOFYEAR(curdate()) +7 >= dayofyear(`bday`) LIMIT 30;
</pre>
<p>There is of course one small problem with this, it&#8217;s not sorted. If we sort by `bday` ASC it&#8217;ll still be off since user&#8217;s are born on different years. What we really want is to sort by month and day only. I&#8217;m sure this can be done in MySQL but here is a PHP solution to take care of it.</p>
<pre name="code" class="php">
$sql = "SELECT * FROM `users` WHERE  DAYOFYEAR(curdate()) <= dayofyear(`bday`) AND DAYOFYEAR(curdate()) +7 >= dayofyear(`bday`) LIMIT 30;";
$query = mysql_query($sql);
$rows = mysql_num_rows($query);
if(!empty($rows)){
	while($resultFind = mysql_fetch_array($query)){
		$arr[] = array('bday' => $resultFind['bday'], 'bday2' => date("md", strtotime($resultFind['bday'])));
	}
}

function compare($x, $y) {
	if ( $x["bday2"] == $y["bday2"] )
	 return 0;
	else if ( $x["bday2"] < $y["bday2"] )
	 return -1;
	else
	 return 1;
}

usort($arr, "compare");
</pre>
<p>Using usort() we can sort based on the month and day. You now have your list sorted <img src='http://www.wrichards.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.wrichards.com/blog/2009/03/getting-upcoming-birthdays-from-mysql/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
