?
Solved

Sort Array Based on Date (PHP)

Posted on 2010-01-10
6
Medium Priority
?
272 Views
Last Modified: 2012-05-08
I have an array, with a key that is a date, like such 'Sun, 10 Jan 2010 00:51:30 +0000' and I want to sort the array so that I get most recent items first. I can make the date any format. I tried making it a timestamp, and then using ksort, but no luck.

0
Comment
Question by:interclubs
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
6 Comments
 
LVL 27

Expert Comment

by:Cornelia Yoder
ID: 26280585
If you are sure you got a valid unix timestamp, then why didn't it work?  What went wrong?
0
 
LVL 82

Expert Comment

by:hielo
ID: 26280588
item at index zero will have the latest item
0
 
LVL 82

Expert Comment

by:hielo
ID: 26280592
sorry, about that. Here you go:
<?php
function sortIt($a,$b){

	$a=strtotime($a);
	$b=strtotime($b);
	
	if($a==$b)return 0;
	return($a<$b?-1:1);
}
$data = array('Sun, 10 Jan 2010 00:51:30 +0000'=>'a'
			,'Sun, 10 Jan 2010 00:51:50 +0000'=>'c'
			,'Sun, 10 Jan 2010 00:51:40 +0000'=>'b'
			);
usort($data,"sortIt");
print_r($data);
?>

Open in new window

0
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
LVL 82

Expert Comment

by:hielo
ID: 26280596
try this instead:
<?php
function sortIt($a,$b){

	$a=strtotime($a);
	$b=strtotime($b);
	
	if($a==$b)return 0;
	return($a<$b?1:-1);
}
$data = array('Sun, 10 Jan 2010 00:51:30 +0000'=>'a'
			,'Sun, 10 Jan 2010 00:51:50 +0000'=>'c'
			,'Sun, 10 Jan 2010 00:51:40 +0000'=>'b'
			);
uksort($data,"sortIt");
print_r($data);
?>

Open in new window

0
 

Author Comment

by:interclubs
ID: 26280673
Hmmmm, what am I missing. I changed the date format to see if that would help. Here is a sample  array:

Array
(
    [0] => Array
        (
            [2010-01-10 16:45:33] => test
        )

    [1] => Array
        (
            [2010-01-10 06:01:02] => test
        )

    [2] => Array
        (
            [2010-01-10 00:51:30] => test
        )

    [3] => Array
        (
            [2010-01-09 22:38:54] => test
        )

    [4] => Array
        (
            [2010-01-09 07:49:00] => test
        )

    [5] => Array
        (
            [2010-01-10 23:38:36] => test
        )

    [6] => Array
        (
            [2010-01-09 19:47:12] => test
        )

    [7] => Array
        (
            [2009-12-31 14:42:29] => test
        )

    [8] => Array
        (
            [2009-12-29 16:07:34] => test
        )

    [9] => Array
        (
            [2009-12-28 14:05:53] => test
        )

)
0
 
LVL 82

Accepted Solution

by:
hielo earned 2000 total points
ID: 26280698
you have an array of arrays. You can't use sortIt() as given.  Change:
        $a=strtotime($a);
        $b=strtotime($b);

to:
$a=strtotime(array_pop(array_keys($a)));
$b=strtotime(array_pop(array_keys($b)));
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question