<?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>ThinkRobot &#187; function</title>
	<atom:link href="http://think-robot.com/tag/function/feed/" rel="self" type="application/rss+xml" />
	<link>http://think-robot.com</link>
	<description>Design &#38; Development Blog</description>
	<lastBuildDate>Tue, 09 Aug 2011 16:56:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Week of the Month in Mysql</title>
		<link>http://think-robot.com/2009/04/week-of-the-month-in-mysql/</link>
		<comments>http://think-robot.com/2009/04/week-of-the-month-in-mysql/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 15:37:16 +0000</pubDate>
		<dc:creator>Joanna</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[Mysql]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[stackoverflow]]></category>
		<category><![CDATA[week]]></category>

		<guid isPermaLink="false">http://think-robot.com/?p=173</guid>
		<description><![CDATA[SELECT WEEK(my_date_field,5) - WEEK(DATE_SUB(my_date_field, INTERVAL DAYOFMONTH(my_date_field)-1 DAY),5)+1 Recently I needed to get the number of the week in a certain month. There is the handy WEEK() function, however it only gives you the week of the year. Obviously before we can go through the code above, a definition of the nth week of the month [...]]]></description>
			<content:encoded><![CDATA[<pre><code>SELECT WEEK(my_date_field,5) -
WEEK(DATE_SUB(my_date_field, INTERVAL DAYOFMONTH(my_date_field)-1 DAY),5)+1
</code></pre>
<p><span id="more-173"></span><br />
Recently I needed to get the number of the week in a certain month. There is the handy <strong>WEEK()</strong> function, however it only gives you the week of the year.</p>
<p>Obviously before we can go through the code above, a definition of the <strong>n<sup>th</sup></strong> week of the month is needed (as I realized after <a href="http://stackoverflow.com/questions/785206/function-for-week-of-the-month-in-mysql">asking the question on stackoverflow</a>&#8230;):</p>
<p>For me the week starts on a Monday, and the first week of the month does not have to be a full week &#8211; if a month starts on a Friday, the Friday to Sunday days count as the first week.</p>
<p>With this out of the way here is a quick explanation of what goes on above.</p>
<pre><code>WEEK(my_date_field,<strong>5</strong>)</code></pre>
<p>First we get the actual week number of the year for the specified date. The second parameter is there to define Monday as the first day of the week (<a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_week">more details in Mysql manual</a>).</p>
<pre><code>DAYOFMONTH(my_date_field)</code></pre>
<p>Next we get the day of the month for the specified date.</p>
<pre><code>DATE_SUB(my_date_field, <strong>DAYOFMONTH(my_date_field)</strong>-1 DAY)</code></pre>
<p>We can use that number with <strong>DATE_SUB</strong> to get the date for the 1<sup>st</sup> day of the month. This in turn can be used to get the week number for the 1<sup>st</sup> week of the month.</p>
<pre><code>SELECT WEEK(my_date_field,5) -
WEEK(<strong>DATE_SUB(my_date_field, INTERVAL DAYOFMONTH(my_date_field)-1 DAY)</strong>,5)+1
</code></pre>
<p>Finally we subtract the first day&#8217;s week from the actual week number which should give us the month&#8217;s week number starting from 0. Thus the final <strong>+1</strong> if you want the count to start from 1.</p>
<div class="zemanta-pixie"><img class="zemanta-pixie-img" src="http://img.zemanta.com/pixy.gif?x-id=55c87db5-a5a4-45e9-92f1-43c731c03ece" /><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>
<div id="crp_related"><h2>Related Articles:</h2><ul><li><a href="http://think-robot.com/2008/06/atmedia-london-2008/" rel="bookmark">@media - London, 2008</a></li><li><a href="http://think-robot.com/2009/02/firefox-ignores-tabs-but-not-spaces-in-a-pre-tag/" rel="bookmark">Firefox ignores tabs but not spaces in a pre tag</a></li><li><a href="http://think-robot.com/2009/02/how-to-use-the-strong-ownership-list/" rel="bookmark">How To Use the Strong Ownership List</a></li><li><a href="http://think-robot.com/2009/12/zend_date-time-part-and-gmt/" rel="bookmark">Zend_Date time part and GMT</a></li><li><a href="http://think-robot.com/2011/03/jquery-multiple-events-without-default-behaviour/" rel="bookmark">jQuery Multiple Events Without Default Behaviour</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://think-robot.com/2009/04/week-of-the-month-in-mysql/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

