<?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>Jeremy Moseley &#187; MySQL</title>
	<atom:link href="http://jeremymoseley.com/tags/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://jeremymoseley.com</link>
	<description>Application Development Blog</description>
	<lastBuildDate>Tue, 13 Jul 2010 06:40:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Dealing with MySQL Date</title>
		<link>http://jeremymoseley.com/2009/10/02/dealing-with-mysql-date/</link>
		<comments>http://jeremymoseley.com/2009/10/02/dealing-with-mysql-date/#comments</comments>
		<pubDate>Fri, 02 Oct 2009 07:48:04 +0000</pubDate>
		<dc:creator>Jeremy</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Solution]]></category>

		<guid isPermaLink="false">http://jeremymoseley.com/?p=25</guid>
		<description><![CDATA[Working with dates can be a pain, especially if you are storing them in a database. In the quick example you will learn the basics of the date, strtotime, and mktime functions to convert your dates to be stored in &#8230; <a href="http://jeremymoseley.com/2009/10/02/dealing-with-mysql-date/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Working with dates can be a pain, especially if you are storing them in a database. In the quick example you will learn the basics of the date, strtotime, and mktime functions to convert your dates to be stored in a MySQL database and to retrieve the date from the database and display it in a nice format for the end user to see.<span id="more-25"></span></p>
<p>Assuming you are using MySQL as your database, a data type of &#8216;date&#8217; is stored as &#8216;YYYY-MM-DD&#8217;. In PHP we can represent the MySQL date data type by using the <a href="http://us3.php.net/manual/en/function.date.php" target="_blank">date()</a> function like so.</p>
<pre class="brush:php;">echo date('Y-m-d'); //2010-10-02</pre>
<p>To convert a stored date in a MySQL database to a user friendly format, use the <a href="http://us3.php.net/manual/en/function.strtotime.php" target="_blank">strtotime()</a> function to convert the date retrieved from the database into seconds. You will use this as the second parameter for the <a href="http://us3.php.net/manual/en/function.date.php" target="_blank">date()</a> function that contains a string of how you want the date formatted. Check out the PHP manual&#8217;s date function page for a full list of what all the letter represent. </p>
<pre class="brush:php;">$db_date = '2010-10-02'; //MySQL date format
echo date('F j, Y', strtotime($db_date)); //October 2, 2010</pre>
<p>When converting dates for entry into a database, you will likely collect the year, month, and date from a form as separate variables. If this is the case you can easily convert the variables into a MySQL ready date by using the <a href="http://us3.php.net/manual/en/function.mktime.php" target="_blank">mktime()</a> function.</p>
<pre class="brush:php;">$year = 2010;
$month = 10;
$day = 2; //notice the 2 is not padded with a zero
$db_date = date('Y-m-d', mktime(0, 0, 0, $month, $day, $year));
echo $db_date; //2010-10-02</pre>
<p>If you are using time in your code, the date function will handle time as well. The corresponding MySQL data type is &#8220;datetime&#8221;.</p>
<pre class="brush:php;">echo date('Y-m-d H:i:s'); //2010-10-02 00:32:15</pre>
]]></content:encoded>
			<wfw:commentRss>http://jeremymoseley.com/2009/10/02/dealing-with-mysql-date/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
