Okay, I get it. I'm a CS student too and some of the work is demanding and frustrating.
First of all, your date_to_timestamp() function isn't right. You need to create a new array with new timestamps, otherwise it won't return anything (which it's not). $newarr is the array that we're going to store the converted array in.
Each conversion using mktime() is pushed into the array $newarr.
You were right in exploding the string with "-" as the glue and inserted the correct array pieces into mktime(), so no problem there. Then you need the function to return $newarr. The argument, $d, is the array to be converted.
PHP Code:
function date_to_timestamp($d){
$newarr = array();
foreach($d as $f) {
$arr=explode("-",$f);
array_push($newarr, mktime(0,0,0,$arr[0],$arr[1],$arr[2]));
}
return $newarr;
}
Now you need to have a sort function (you called it cmp2(), perfectly fine). What you wrote is correct, no need to change anything there.
PHP Code:
function cmp2($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
Finally, store the array of $dates converted using date_to_timestamp() into a new variable (in this case, $third) and use usort() to sort $third using cmp2 as the callback. Then print it.
PHP Code:
$third = date_to_timestamp($dates);
usort($third, "cmp2");
echo "<pre>";
print_r($third);
echo "</pre>";
And there you go. I hope you understood what I did, I tested it and it works perfect.
Bookmarks