<?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>Random Snippets &#187; php</title>
	<atom:link href="http://www.randomsnippets.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.randomsnippets.com</link>
	<description>Random Snippets of Code and Ideas for any Web Developer</description>
	<lastBuildDate>Wed, 05 Aug 2009 14:32:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=abc</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Sorting 2D associative arrays in PHP</title>
		<link>http://www.randomsnippets.com/2009/07/13/sorting-2d-associative-arrays-in-php/</link>
		<comments>http://www.randomsnippets.com/2009/07/13/sorting-2d-associative-arrays-in-php/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 06:18:52 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[php]]></category>
		<category><![CDATA[multi-dimensional associative array]]></category>
		<category><![CDATA[sort]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=238</guid>
		<description><![CDATA[Surprisingly, it took me a long time to find this solution so I decided to post it for anyone who had a situation similar to mine.
Let&#8217;s say you have the following 2D associative array in PHP of fruits and their corresponding prices and you want to have them sorted by price:

$fruits = array
&#40;
   [...]]]></description>
			<content:encoded><![CDATA[<p>Surprisingly, it took me a long time to find this solution so I decided to post it for anyone who had a situation similar to mine.</p>
<p>Let&#8217;s say you have the following 2D associative array in PHP of fruits and their corresponding prices and you want to have them sorted by price:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$fruits</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span><span style="color:#800080;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="">'product'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="">'banana'</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.99</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="">'product'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="">'apple'</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">1.99</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="">'product'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="">'durian'</span><span style="color: #339933;">,</span>  <span style="color: #666666; font-style: italic;">//these smell by the way - i do not know how people can like them =)</span>
            <span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">19.99</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="">'product'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="">'starfruit'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">//not too stinky but still stinky nonetheless</span>
            <span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span>     <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">5.99</span>
        <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p><span id="more-238"></span><br />
The answer to this one involves the use of the PHP function, <strong>usort</strong>, which allows users to define their own comparison function for sorting arrays of all shapes and sizes.</p>
<p>For this example, our function would need to be defined as the following if we wanted to have the prices descending:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sortDescending <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color:#800080;">0</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is the slightly different version of the function for an ascending sort:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sortAscending <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color:#800080;">0</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>To put it all together in a descending sort, we have the following:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> sortDescending <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color:#800080;">0</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> ? <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span>;
<span style="color: #009900;">&#125;</span>
<span style="color: #990000;">usort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fruits</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;sortDescending&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$fruits</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$fruit</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">echo</span> <span style="color: #000088;">$fruit</span><span style="color: #009900;">&#91;</span><span style="">'product'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$fruit</span><span style="color: #009900;">&#91;</span><span style="">'price'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This would give us the following output:<br />
<code><br />
durian: 19.99<br />
starfruit: 5.99<br />
banana: 2.99<br />
apple: 1.99<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2009/07/13/sorting-2d-associative-arrays-in-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to convert MySQL timestamp to PHP date type</title>
		<link>http://www.randomsnippets.com/2008/10/05/how-to-convert-mysql-timestamp-to-php-date-type/</link>
		<comments>http://www.randomsnippets.com/2008/10/05/how-to-convert-mysql-timestamp-to-php-date-type/#comments</comments>
		<pubDate>Mon, 06 Oct 2008 05:01:52 +0000</pubDate>
		<dc:creator>Knix</dc:creator>
				<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[date function]]></category>
		<category><![CDATA[hh mm ss]]></category>
		<category><![CDATA[mysql timestamp]]></category>
		<category><![CDATA[parsing]]></category>
		<category><![CDATA[php date]]></category>
		<category><![CDATA[php strtotime]]></category>
		<category><![CDATA[query]]></category>
		<category><![CDATA[timestamp]]></category>
		<category><![CDATA[unix timestamp]]></category>

		<guid isPermaLink="false">http://www.randomsnippets.com/?p=131</guid>
		<description><![CDATA[Let&#8217;s say you have the following PHP code that extracts the date from the times table in your MySQL database.  The date is of timestamp type which has the following format: &#8216;YYYY-MM-DD HH:MM:SS&#8217; or &#8216;2008-10-05 21:34:02.&#8217;

1
2
3
4
$res = mysql_query&#40;&#34;SELECT date FROM times;&#34;&#41;;
while &#40; $row = mysql_fetch_array&#40;$res&#41; &#41; &#123;
   echo $row&#91;'date'&#93; . &#34;&#60;br /&#62;&#34;;
&#125;


This [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say you have the following PHP code that extracts the <strong>date</strong> from the <strong>times</strong> table in your MySQL database.  The <strong>date</strong> is of <strong>timestamp</strong> type which has the following format: &#8216;YYYY-MM-DD HH:MM:SS&#8217; or &#8216;2008-10-05 21:34:02.&#8217;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT date FROM times;&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #990000;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="">'date'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span>;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><span id="more-131"></span><br />
This date format that is in the output is in the timestamp format, <code>2008-10-05 21:34:02</code>, which is not surprising, but you want something that is more &#8220;user-friendly&#8221; or &#8220;readable&#8221; as in <strong>&#8220;9:34 pm October 5, 2008.&#8221;</strong></p>
<p>Let&#8217;s go back to the drawing board and try again:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000088;">$res</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;SELECT date FROM times;&quot;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$res</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;g:i a F j, Y &quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;&quot;</span>;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>A sample output from this PHP code would be <code>9:34 pm October 5, 2008</code> which is much more user-friendly.</p>
<p>The PHP <strong>strtotime</strong> function parses the MySQL timestamp into a Unix timestamp which can be utilized for further parsing or formatting in the PHP <strong>date</strong> function.</p>
<p>Here are some other sample date output formats that may be of practical use:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;F j, Y g:i a&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;                  <span style="color: #666666; font-style: italic;">// October 5, 2008 9:34 pm</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;m.d.y&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;                         <span style="color: #666666; font-style: italic;">// 10.05.08</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;j, n, Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;                       <span style="color: #666666; font-style: italic;">// 5, 10, 2008</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Ymd&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;                           <span style="color: #666666; font-style: italic;">// 20081005</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="">'\i\t \i\s \t\h\e jS \d\a\y.'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;   <span style="color: #666666; font-style: italic;">// It is the 5th day.</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;D M j G:i:s T Y&quot;</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;date&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;               <span style="color: #666666; font-style: italic;">// Sun Oct 5 21:34:02 PST 2008</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.randomsnippets.com/2008/10/05/how-to-convert-mysql-timestamp-to-php-date-type/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
