<?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>The Journal of Tim Roberts &#187; MySQL</title>
	<atom:link href="http://www.wiseguysonly.com/category/web-development/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wiseguysonly.com</link>
	<description>PHP,MySQL and jQuery Developer</description>
	<lastBuildDate>Sun, 20 Jun 2010 17:12:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tips for Manipulating dates in MySQL.</title>
		<link>http://www.wiseguysonly.com/2009/02/06/tips-for-manipulating-dates-in-mysql/</link>
		<comments>http://www.wiseguysonly.com/2009/02/06/tips-for-manipulating-dates-in-mysql/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 19:52:07 +0000</pubDate>
		<dc:creator>Tim</dc:creator>
				<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://www.wiseguysonly.com/?p=197</guid>
		<description><![CDATA[Bypassing PHP to manipulate dates.
I despise working with dates in PHP. So much so that I now manipulate my dates using MySQL so I have everything I want in a php variable delivered by one SQL statement. Adding, subtracting, calculating differences and performing other date manipulations can be a pain in PHP. If you have [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Bypassing PHP to manipulate dates.</strong></p>
<p>I despise working with dates in PHP. So much so that I now manipulate my dates using MySQL so I have everything I want in a php variable delivered by one SQL statement. Adding, subtracting, calculating differences and performing other date manipulations can be a pain in PHP. If you have a database connection running in your application it can be much quicker and easier to read if you work with dates in your SQL. The key to this is that you <em>don&#8217;t</em> need to work with tables and records in a database to use SQL. Many of the examples below don&#8217;t make queries against any record sets and yet they return valid results.<span id="more-197"></span></p>
<p><strong>Adding or subtracting time from dates in MySQL.</strong></p>
<p>Often when I work with PayPal subscriptions I have to update a users renewal date on my database by say 6 months etc. MySQL has a very simple way of dealing with this in the INTERVAL statement. For example to add 1 month to todays date you can do this:</p>
<p>SELECT NOW() + INTERVAL 1 MONTH</p>
<p>Conversely to subtract 5 days from the current date do the following:</p>
<p>SELECT NOW() &#8211; 5 DAY</p>
<p>Let&#8217;s say you want to add a year to a subscribtion by updating the field renewal_date:</p>
<p>UPDATE users SET renewal date = renewal_date + INTERVAL 1 YEAR</p>
<p>There are good few timescales that can be used with INTERVAL including SECOND, MINUTE, DAY, WEEK, MONTH, YEAR.</p>
<p><strong>Formatting dates in MySQL.</strong></p>
<p>MySQL uses the American date format of YYY-MM-DD. I don&#8217;t care for it any more than I care for using the property &#8220;color&#8221; in CSS, but I can&#8217;t change it. To change the way a date appears in MySQL, the true power-tool is DATE_FORMAT. This takes 2 arguments: the date you want to format and the new appearance of this date.</p>
<p>For instance:</p>
<p>SELECT DATE_FORMAT(NOW(),&#8217;%d/%m/%Y&#8217;) as display_date</p>
<p>Would return something like:</p>
<p>display_date=&gt;27/03/2007</p>
<p>Of  course, in this instance PHP&#8217;s date() function would be just as easy to use, but when you are dealing with a date from a field DATE_FORMAT is irreplaceable.</p>
<p><a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format">A full list of date formatting codes is avaliable in the MySQL Reference Manual</a>.</p>
<p><strong>Calculating the difference between two dates in MySQL</strong></p>
<p>Another simple task when you use the DATEDIFF function. The first argument is the start date, and the second is the date you want to calcuate the difference to or from:</p>
<p>SELECT DATEDIFF(&#8221;2007-12-31&#8243;,&#8221;2007-12-30&#8243;)</p>
<p>Gives us a value of 1.</p>
<p>SELECT DATEDIFF(&#8221;2007-12-28&#8243;,&#8221;2007-12-31&#8243;)</p>
<p>Give us a value of -3</p>
<p>The results of DATEDIFF are given in days.</p>
<p><strong>Selecting parts of a date in MySQL.</strong></p>
<p>You may want to return a part of a date such as the day that a specific date falls on, or the hour that a task is due on a certain day. There are a number of functions to get specific parts of a date. All you have to do is feed them with a date:</p>
<p>SELECT DAYNAME(&#8221;2004-02-13&#8243;)</p>
<p>Would return &#8220;Friday&#8221;</p>
<p>Other useful functions for extracting date parts are:</p>
<p>DAY() &#8211; returns the day of the month (e.g 13)<br />
MONTH() &#8211; returns the month name<br />
YEAR() &#8211; returns the year<br />
WEEK() &#8211; returns the week number</p>
<p><strong>Digging Deeper</strong></p>
<p>All the above are very simple techniques that will make your day to day use of dates in your coding more pleasant. However, they are the tip of the iceberg in MySQL date manipulation and should you wish to go further, th<a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html">e MySQL reference manual has a comprehensive section on date functions.</a></p>
<pre class="programlisting"><strong class="userinput"></strong></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.wiseguysonly.com/2009/02/06/tips-for-manipulating-dates-in-mysql/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
