<?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>Andy Weigel &#187; dates</title>
	<atom:link href="http://andyweigel.com/blog/tag/dates/feed" rel="self" type="application/rss+xml" />
	<link>http://andyweigel.com/blog</link>
	<description>Web Development Tutorials</description>
	<lastBuildDate>Wed, 27 Oct 2010 00:21:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Convert a String into a Date with PHP</title>
		<link>http://andyweigel.com/blog/php-tutorials/convert-a-string-into-a-date-with-php/47</link>
		<comments>http://andyweigel.com/blog/php-tutorials/convert-a-string-into-a-date-with-php/47#comments</comments>
		<pubDate>Wed, 30 Sep 2009 19:40:44 +0000</pubDate>
		<dc:creator>Andy</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[dates]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://andyweigel.com/blog/?p=47</guid>
		<description><![CDATA[This problem came up while working with the Allegheny County Property database. They have a field for the sale date of the property, only they&#8217;re storing the date like this &#8211; 3291982 for 03/29/1982. The problem is that there is no leading zero for the month, so using substr function and counting 2, 2 and [...]]]></description>
			<content:encoded><![CDATA[<p>This problem came up while working with the Allegheny County Property database. They have a field for the sale date of the property, only they&#8217;re storing the date like this &#8211; 3291982 for 03/29/1982. </p>
<p><span id="more-47"></span></p>
<p>The problem is that there is no leading zero for the month, so using<a href="http://www.php.net/substr" target="_blank"> substr function</a> and counting 2, 2 and 4 won&#8217;t work in every situation. Here&#8217;s what I did to create a human readable date (03/29/1982) as well as a UNIX time variable.</p>
<h3>The Functions</h3>
<p>For this conversion I&#8217;m going to use three functions -<a href="http://www.php.net/substr" target="_blank"> substr</a>, str_replace and <a href="http://www.php.net/manual/en/function.strtotime.php" target="_blank">strtotime</a>. Here&#8217;s a quick breakdown of how they work.</p>
<p><strong>substr ( string $string     , int $start     [, int $length    ] )</strong> &#8211; this returns a portion of the string based on the <em>start</em> and <em>length</em> parameters. You can use negative numbers for the<em> start</em> to pull from the end of the string, which is what we&#8217;ll be doing.</p>
<p><strong>str_replace ( <a href="http://www.php.net/manual/en/language.pseudo-types.php#language.types.mixed">mixed</a> $search     , <a href="http://www.php.net/manual/en/language.pseudo-types.php#language.types.mixed">mixed</a> $replace     , <a href="http://www.php.net/manual/en/language.pseudo-types.php#language.types.mixed">mixed</a> $subject)</strong> &#8211; this function will return a string or an array with all occurences of the <em>search</em> parameter in the <em>subject</em> parameter replaced with the <em>replace</em> parameter.</p>
<p><strong>strtotime ( string $date )</strong> &#8211; this function takes a US English date format and will parse it into a UNIX timestamp. </p>
<h3>Down to Business</h3>
<pre class="php">
<span class="phpComment">// Takes a <span class="phpFunction">date</span> <span class="phpOperator">(</span>11222009<span class="phpOperator">)</span> and returns 11/22/2009
</span><span class="phpFunctionKeyword">function</span> fix_<span class="phpFunction">date</span><span class="phpOperator">(</span>$sale_<span class="phpFunction">date</span><span class="phpOperator">)</span>
<span class="phpOperator">{</span>
	$sale_date_year <span class="phpOperator">=</span> <span class="phpFunction">substr</span> <span class="phpOperator">(</span>$sale_<span class="phpFunction">date</span>, -<span class="phpNumber">4</span><span class="phpOperator">)</span><span class="phpText">;</span>
	$sale_date_day <span class="phpOperator">=</span> <span class="phpFunction">substr</span> <span class="phpOperator">(</span>$sale_<span class="phpFunction">date</span>, -<span class="phpNumber">6</span>, <span class="phpNumber">2</span><span class="phpOperator">)</span><span class="phpText">;</span>
	$string_to_replace <span class="phpOperator">=</span> $sale_date_day <span class="phpOperator">.</span> $sale_date_year<span class="phpText">;</span>
	$sale_date_month <span class="phpOperator">=</span> <span class="phpFunction">str_replace</span><span class="phpOperator">(</span>$string_to_replace,<span class="phpString">''</span>,$sale_<span class="phpFunction">date</span><span class="phpOperator">)</span><span class="phpText">;</span>
	$sale_date_clean <span class="phpOperator">=</span> <span class="phpString">"$sale_date_month/$sale_date_day/$sale_date_year"</span><span class="phpText">;</span>
<span class="phpKeyword">	if </span><span class="phpOperator">(</span>$sale_date_clean <span class="phpOperator"><span class="phpOperator">=</span>=</span> <span class="phpString">"<span class="phpComment">//"</span><span class="phpOperator">)</span> <span class="phpOperator">{</span>$sale_date_clean <span class="phpOperator">=</span> NULL<span class="phpText">;</span><span class="phpOperator">}</span>
</span>
<span class="phpKeyword">	return </span>$sale_date_clean<span class="phpText">;</span>
<span class="phpOperator">}</span>
</pre>
<p>I pulled the sale date from the database and created the $sale_date variable out of it. For this example lets suppose that $sale_date = 3291982.</p>
<h5>Let&#8217;s Breakout the Year</h5>
<p>Next, I figured that the last 4 digits will always be the year. I was able to create a variable from them using the substr function that&#8217;s provided by PHP. What this function is doing is telling it to take the $sale_date and take the last 4 numbers off the end (-4)for a total of 4 numbers (4).</p>
<pre class="php">
$sale_date_year <span class="phpOperator">=</span> <span class="phpFunction">substr</span> <span class="phpOperator">(</span>$sale_date, -<span class="phpNumber">4</span>, <span class="phpNumber">4</span><span class="phpOperator">)</span><span class="phpText">;</span>
</pre>
<p><strong>So at this point $sale_date_year = 1982.</strong></p>
<h5>Getting the Day of the Month</h5>
<p>The next step was to strip out the day of the month using the same substr function &#8211; this time telling it to take 6 numbers from the end (-6) for a total of 2 numbers (2).</p>
<pre class="php">
 $sale_date_day <span class="phpOperator">=</span> <span class="phpFunction">substr</span> <span class="phpOperator">(</span>$sale_date, -<span class="phpNumber">6</span>, <span class="phpNumber">2</span><span class="phpOperator">)</span><span class="phpText">;</span>
</pre>
<p><strong>This function will return $sale_date_day = 29. </strong></p>
<h5>Now for the Tricky Part&#8230; </h5>
<p>I&#8217;m going to combine the 2 new variables together to create a string to use as my replace variable to use the str_replace function.</p>
<pre class="php">
$string_to_replace <span class="phpOperator">=</span> $sale_date_day <span class="phpOperator">.</span> $sale_date_year<span class="phpText">;</span>
</pre>
<p>At this point $string_to_replace = 291982. I&#8217;m going to use this in the str_replace function to get the month from the $sale_date variable.Here&#8217;s the function -</p>
<pre class="php">
$sale_date_month <span class="phpOperator">=</span> <span class="phpFunction">str_replace</span><span class="phpOperator">(</span>$string_to_replace,<span class="phpString">''</span>,$sale_date<span class="phpOperator">)</span><span class="phpText">;</span>
</pre>
<p>This will return $sale_date_month = 3</p>
<h5>Creating a User-Friendly Date</h5>
<p>The next step is to create a human readable date. To this, I simply combine all the separate variables into one variable, separated with a slash.</p>
<pre class="php">
$sale_date_clean <span class="phpOperator">=</span> <span class="phpString">"$sale_date_month/$sale_date_day/$sale_date_year"</span><span class="phpText">;</span>
</pre>
<p>The $sale_date_clean will now look like this &#8211; 3/29/1982.</p>
<h5>Make the Pretty Date into a UNIX Timestamp</h5>
<p>The final step will be to take the $sale_date_clean that we just created and make it into a UNIX timestamp. We&#8217;ll use the strtotime function of this.</p>
<pre class="php">
$unix_date <span class="phpOperator">=</span> <span class="phpFunction">strtotime</span><span class="phpOperator">(</span>$sale_date_clean<span class="phpOperator">)</span><span class="phpText">;</span>
</pre>
<p>At this point we have 2 clean variables to do whatever you want with.</p>
<p>Hopefully you&#8217;ve found this quick tutorial valuable. <a href="contact.php">Contact me</a> if you have any questions or would do it a different way. There&#8217;s 1,001 ways to do everything &#8211; that&#8217;s why I love coding.</p>
]]></content:encoded>
			<wfw:commentRss>http://andyweigel.com/blog/php-tutorials/convert-a-string-into-a-date-with-php/47/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

