[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Sort Array Based on Date (PHP)

Posted on 2010-01-10
6
Medium Priority
?
274 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
  • 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
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

612 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