Date array into unix mktime

Hi:

I’m looking to sort an array of dates in format 10-10-2003 using usort after they have been converted using mktime.

mktime will only work on 1 string… how can I convert the entire array and then process through my usort? I have so far:

[php]$dates = array(‘10-10-2003’, ‘2-17-2002’, ‘2-16-2003’, ‘1-01-2005’, ‘10-10-2004’);

mktime($dates);

function cmp($a, $b){
return strcmp($b[‘db’], $a[‘db’]);
}

usort($dates, “cmp”);
print_r($dates);
[/php]

To be clear I want to Use usort and sort $date by the timestamp converted by mktime

There are many array sorting functions in PHP, not sure why you want to use usort() for your task. I would suggest to use array_multisort() - it will sort one array by given another array. Here is the code:
[php]<?php

$dates = array(‘10-10-2003’, ‘2-17-2002’, ‘2-16-2003’, ‘1-01-2005’, ‘10-10-2004’);

function date_to_timestamp($d){
$arr=explode("-",$d);
return mktime(0,0,0,$arr[0],$arr[1],$arr[2]);
}

$timestamps = array_map(“date_to_timestamp”, $dates);

array_multisort($timestamps, $dates);

echo ‘

’;
print_r($dates);
echo ‘
’;

?>[/php]

Wow thanks…

I was really stuck on how to get the array converted into timestamps!

I’ll try to figure out the usort part next… it’s a requirement for the class I’m taking. :slight_smile:

The issue I have with that (which is also a problem in the other larger part of my code-this is only a small part) is that usort only compares the first 2 parts of the date and not the 3rd- In other words 10-10-2003 and 10-10-2004 look the same and don’t sort correctly?

Sponsor our Newsletter | Privacy Policy | Terms of Service