<?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>Flipflops.org &#187; MySQL</title>
	<atom:link href="http://www.flipflops.org/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.flipflops.org</link>
	<description>Flipflops.org is about web development and fairly conceptual art</description>
	<lastBuildDate>Tue, 31 Jan 2012 23:26:16 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>MySQL DATE_FORMAT() AND PHP date() formats</title>
		<link>http://www.flipflops.org/2009/12/01/mysql-date_format-and-php-date-formats/</link>
		<comments>http://www.flipflops.org/2009/12/01/mysql-date_format-and-php-date-formats/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 20:21:11 +0000</pubDate>
		<dc:creator>Flipflops</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Datetime]]></category>

		<guid isPermaLink="false">http://www.flipflops.org/2009/12/01/mysql-date_format-and-php-date-formats/</guid>
		<description><![CDATA[I&#8217;m always finding myself looking up strings to format PHP and MySQL dates. A simple d/m/Y is pretty straight forward but sometimes things can get a bit fiddly. Where ever possible I try and leave my date formatting in the database, but I still find myself doing it in PHP all the time. Here are [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m always finding myself looking up strings to format PHP and MySQL dates. A simple d/m/Y is pretty straight forward but sometimes things can get a bit fiddly. Where ever possible I try and leave my date formatting in the database, but I still find myself doing it in PHP all the time.</p>
<p>Here are a few simple cut and paste date formats.</p>
<table class="exam_table mtable" >
<thead>
<tr>
<th scope="col">PHP Date</th>
<th scope="col">MySQL Date</th>
<th scope="col">PHP Format</th>
<th scope="col">MySQL Format</th>
<th scope="col">Notes</th>
</tr>
<thead>
<tbody>
<tr>
<td>7/9/2009</td>
<td>7/9/2009</td>
<td>j/n/Y</td>
<td>%e/%c/%Y</td>
<td></td>
</tr>
<tr>
<td>07/09/2009</td>
<td>07/09/2009</td>
<td>d/m/Y</td>
<td>%d/%m/%Y</td>
<td></td>
</tr>
<tr>
<td>7/9/2009 8:07</td>
<td>7/9/2009 8:07</td>
<td>j/n/Y G:i</td>
<td>%e/%c/%Y %k:%i</td>
<td></td>
</tr>
<tr>
<td>7/9/2009 8:07 AM</td>
<td>7/9/2009 8:07 AM</td>
<td>j/n/Y G:i A</td>
<td>%e/%c/%Y %k:%i %p</td>
<td></td>
</tr>
<tr>
<td>07/09/2009 8:07 AM</td>
<td>07/09/2009 8:07 AM</td>
<td>d/m/Y G:i A</td>
<td>%d/%m/%Y %k:%i %p</td>
<td></td>
</tr>
<tr>
<td>07/09/2009 08:07 AM</td>
<td>07/09/2009 08:07 AM</td>
<td>d/m/Y H:i A</td>
<td>%d/%m/%Y %H:%i %p</td>
<td></td>
</tr>
<tr>
<td>07/09/2009 8:07 am</td>
<td></td>
<td>d/m/Y G:i a</td>
<td></td>
<td></td>
</tr>
<tr>
<td>Mon 7th Sep 2009</td>
<td>Mon 7th Sep 2009</td>
<td>D jS M Y</td>
<td>%a %D %b %Y</td>
<td></td>
</tr>
<tr>
<td>Monday 7th September 2009</td>
<td>Monday 7th September 2009</td>
<td>l jS F Y</td>
<td>%W %D %M %Y</td>
<td></td>
</tr>
<tr>
<td>2009-09-07T08:07:50+01:00</td>
<td>Monday 7th September 2009</td>
<td>Y-m-d\TH:i:sP</td>
<td>%W %D %M %Y</td>
<td>PHP DATE_ATOM constant</td>
</tr>
<tr>
<td>Mon, 07 Sep 2009 08:07:50 +0100</td>
<td>Mon, 07 Sep 2009 08:07:50</td>
<td>D, d M Y H:i:s O</td>
<td>%a, %d %b %Y %T</td>
<td>PHP DATE_RSS constant</td>
</tr>
</tbody>
</table>
<h2>The PHP way</h2>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">&nbsp;
<span style="color: #b1b100;">echo</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'d/m/Y'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'2009-09-07 08:07:50'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//or</span>
&nbsp;
<span style="color: #000088;">$dateTime</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DateTime<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;now&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$dateTime</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">format</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Y-m-d H:i:s&quot;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<h2>The MySQL way</h2>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">&nbsp;
$sqlstring <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">&quot;SELECT DATE_FORMAT('2009-09-07 08:07:50', '%d/%m/%Y') AS formated_date FROM some_table&quot;</span> ;</pre></div></div>

<ul>
<li><a href="http://php.net/manual/en/function.date.php">http://php.net/manual/en/function.date.php</a></li>
<li><a href="http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format">http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-format</a></li>
</ul>
<p>If you haven&#8217;t looked at the PHP manual closely for a while you should definitely (re)acquaint yourself with the <a href="http://www.php.net/manual/en/class.datetime.php#datetime.constants.types">DateTime Class</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.flipflops.org/2009/12/01/mysql-date_format-and-php-date-formats/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>UK Towns and Postcode Areas</title>
		<link>http://www.flipflops.org/2008/11/18/uk-towns-and-postcode-areas/</link>
		<comments>http://www.flipflops.org/2008/11/18/uk-towns-and-postcode-areas/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 19:55:04 +0000</pubDate>
		<dc:creator>Flipflops</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Stuff]]></category>
		<category><![CDATA[Web Things]]></category>

		<guid isPermaLink="false">http://www.flipflops.org/2008/11/18/uk-towns-and-postcode-areas/</guid>
		<description><![CDATA[The other day I needed a list of UK post towns matched up to postcode areas, which I found on wikipedia. Anyway I have tidied it up and put it into two database tables postcode and town You can download the SQL file here towns.zip Hope it comes in useful.]]></description>
			<content:encoded><![CDATA[<p>The other day I needed a list of UK post towns matched up to postcode areas, which I found on <a href="http://en.wikipedia.org/wiki/List_of_post_towns_in_the_United_Kingdom">wikipedia</a>. </p>
<p>Anyway I have tidied it up and put it into two database tables <strong>postcode</strong> and <strong>town</strong></p>
<p><a href='http://www.flipflops.org/wp-content/uploads/2008/11/towns.zip' title='towns.zip'>You can download the SQL file here <strong>towns.zip</strong></a></p>
<p>Hope it comes in useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flipflops.org/2008/11/18/uk-towns-and-postcode-areas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Finding overlapping dates and times in MySQL</title>
		<link>http://www.flipflops.org/2008/05/01/finding-overlapping-dates-and-times-in-mysql/</link>
		<comments>http://www.flipflops.org/2008/05/01/finding-overlapping-dates-and-times-in-mysql/#comments</comments>
		<pubDate>Thu, 01 May 2008 21:04:40 +0000</pubDate>
		<dc:creator>Flipflops</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Web Things]]></category>

		<guid isPermaLink="false">http://www.flipflops.org/2008/05/01/finding-overlapping-dates-and-times-in-mysql/</guid>
		<description><![CDATA[Quite often you end up in situations where you are required to check and see if one time period overlaps another time period. Probably the most common situation this occurs in is when you are building a booking system &#8211; be it for tables or cars or rooms. You have a series of entries in [...]]]></description>
			<content:encoded><![CDATA[<p>Quite often you end up in situations where you are required to check and see if one time period overlaps another time period. Probably the most common situation this occurs in is when you are building a booking system &#8211; be it for tables or cars or rooms.</p>
<p>You have a series of entries in a database with a start (date)time and an end (date)time and before adding a new record you need to check that it doesn&#8217;t overlap with another booking.</p>
<p>The solution is fairly simple but I always end up making a quick diagram on a piece of paper to check.</p>
<p><img src='http://www.flipflops.org/wp-content/uploads/2008/05/mysql_times.gif' width="300px" alt='Time overlapping diagram' /></p>
<p>As you can see two time periods can either be sequential (i.e. there is no overlap at all) or they can overlap in one of four ways. Put this into a diagram and the solution becomes pretty obvious.</p>
<p>There is an overlap if <strong>end_time_1 > start_time_2 AND start_time_1 < end_time_2</strong></p>
<table class="exam_table mtable">
<tr>
<th scope="col">Time</th>
<th scope="col">start_1 </th>
<th scope="col">start_2 </th>
<th scope="col">end_1 </th>
<th scope="col">end_2 </th>
<th scope="col">end_1 &gt; start_2 </th>
<th scope="col">start_1 &lt; end_2 </th>
</tr>
<tr>
<th scope="row">1</th>
<td>09:00</td>
<td>08:00</td>
<td>11:00</td>
<td>10:00</td>
<td>true</td>
<td>true</td>
</tr>
<tr>
<th scope="row">2</th>
<td>09:00</td>
<td>10:00</td>
<td>11:00</td>
<td>12:00</td>
<td>true</td>
<td>true</td>
</tr>
<tr>
<th scope="row">3</th>
<td>09:00</td>
<td>08:00</td>
<td>11:00</td>
<td>12:00</td>
<td>true</td>
<td>true</td>
</tr>
<tr>
<th scope="row">4</th>
<td>09:00</td>
<td>09:30</td>
<td>11:00</td>
<td>10:30</td>
<td>true</td>
<td>true</td>
</tr>
<tr>
<th scope="row">5</th>
<td>09:00</td>
<td>07:00</td>
<td>11:00</td>
<td>08:00</td>
<td>false</td>
<td>true</td>
</tr>
<tr>
<th scope="row">6</th>
<td>09:00</td>
<td>12:00</td>
<td>11:00</td>
<td>13:00</td>
<td>true</td>
<td>false</td>
</tr>
</table>
<p>An example SQL query would be something like the one below but make sure your <a href="http://dev.mysql.com/doc/refman/5.0/en/datetime.html">datetime formats are correct</a> &#8211; in MySQL the default format is YYYY-MM-DD hh:mm:ss</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;">&nbsp;
<span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #CC0099;">*</span> <span style="color: #990099; font-weight: bold;">FROM</span> bookings <span style="color: #990099; font-weight: bold;">WHERE</span> room_id <span style="color: #CC0099;">=</span> <span style="color: #008000;">'&quot; . $room<span style="color: #008080; font-weight: bold;">_</span>id . &quot;'</span> <span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #FF00FF;">&#40;</span>date_end <span style="color: #CC0099;">&gt;</span> <span style="color: #008000;">'&quot; . $start<span style="color: #008080; font-weight: bold;">_</span>date . &quot;'</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #FF00FF;">&#40;</span>date_start <span style="color: #CC0099;">&lt;</span> <span style="color: #008000;">'&quot; . $end<span style="color: #008080; font-weight: bold;">_</span>date . &quot;'</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #008000;">&quot;;</span></pre></div></div>

<p>The above query will pull back the records where the times overlap.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.flipflops.org/2008/05/01/finding-overlapping-dates-and-times-in-mysql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>MySQL import / export at the Command Line</title>
		<link>http://www.flipflops.org/2008/03/19/mysql-import-export-at-the-command-line/</link>
		<comments>http://www.flipflops.org/2008/03/19/mysql-import-export-at-the-command-line/#comments</comments>
		<pubDate>Wed, 19 Mar 2008 21:11:10 +0000</pubDate>
		<dc:creator>Flipflops</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Web Things]]></category>

		<guid isPermaLink="false">http://www.flipflops.org/2008/03/19/mysql-import-export-at-the-command-line/</guid>
		<description><![CDATA[Like many (most?) developers when I work with a database I tend to manage them using another programme &#8211; usually either phpMyAdmin or Navicat. However there are times when you have to get basic again and dig down to the Command Line (usually because of an access issue like a firewall setting you can&#8217;t change [...]]]></description>
			<content:encoded><![CDATA[<p>Like many (most?) developers when I  work with a database I tend to manage them using another programme &#8211; usually either  <a href="http://www.phpmyadmin.net">phpMyAdmin</a> or <a href="http://www.navicat.com/">Navicat</a>. However there are times when you have to get basic again and dig down to the Command Line (usually because of an access issue like a firewall setting you can&#8217;t change or because you need to import a file too big for <a href="http://www.phpmyadmin.net">phpMyAdmin</a>). The trouble is if you only use these commands once in a blue moon you soon forget them.</p>
<h3>Dump (export your database)</h3>
<ol>
<li>Login to the shell or Command Prompt, navigate to where you would like your file saved.</li>
<li><code><br />
mysqldump --user=your_username --password=your_password your_database > your_dump_file.sql<br />
</code></li>
</ol>
<p>Where:<br />
your_username is a mysql user with the correct rights to the database you want to dump.<br />
your_password is the password for your_username.<br />
your_database is the name of your database.<br />
your_dump_file.sql is the name of the file that will be created in the export.</p>
<h3>Import</h3>
<ol>
<li>Create the database that you want to import into, but leave it empty or if you are using an existing database remove the tables (make sure you have a backup first).</li>
<li>Login to the shell or Command Prompt</li>
<li><code><br />
mysql --user=your_username --password=your_password your_database < your_file_to_import.sql<br />
</code></li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.flipflops.org/2008/03/19/mysql-import-export-at-the-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

