× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Sorting an array by UK formatted date string value in PHP

Dominic Myers
Sep 25, 2014
<pre><code class="language-php">function cmp($a, $b) { $a_int = DateTime::createFromFormat('j/m/Y', $a["date"])-&gt;getTimestamp(); $b_int = DateTime::createFromFormat('j/m/Y', $b["date"])-&gt;getTimestamp(); return ($a_int == $b_int) ? 0 : ($a_int &lt; $b_int) ? 1 : -1; }</code></pre><p>This will allow you to sort an array with dates which are formatted like this: DD/MM/YYYY. It can be used on array with "date" keys like this:</p><pre><code class="language-php">&lt;?php usort($arr, "cmp");</code></pre><p> </p>
comments powered by Disqus