PDA

View Full Version : USORT on timestamps



valkyriesound
08-22-2011, 12:43 AM
Greetings:

I'm trying to:
1. Covert an array of dates to timestamps using mktime
2. Usort that array acending

It's not sorting at all.. I think I have an issue in my compare function "cmp2".

I need to use usort- no strotime, no array multisort

Can anyone help me? I'm completely stuck.. I've searched google and didn't see a compare function for usort.

Thanks!





$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]);
}

function cmp2($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$timestamps=array("date_to_timestamp");
usort($timestamps,"cmp2");

djr33
08-22-2011, 01:51 AM
Look carefully at your line here: $timestamps=array("date_to_timestamp"); . What is that actually supposed to be doing?
Don't you want this? $timestamps=array(date_to_timestamp($dates));

However, that still won't actually do it because that will give you an error-- inserting an array into a function that is expecting a single date.

You'll need to make a loop (foreach is a good option) that will iterate through each date and for each return it as a timestamp through the date_to_timestamp function.


From your phrasing it sounds like this is a homework assignment. It is our policy to not do homework for anyone since the entire point is learning (not having answers given to you). We may give some basic pointers, though.



Regarding your uncertainty about the sorting function, check the PHP manual here:
http://php.net/manual/en/function.usort.php
You'll find that looking up any (standard) function on that site is extremely helpful with examples (most of the time, for any common function at least).
But it looks like your compare function is correct.

JShor
08-22-2011, 03:13 PM
Why not just use sort()?

Here's what to do:
1. Loop through the array.
2. For each item in the array, convert the string into UNIX time using strtotime().
3. Sort the final array using sort() with the SORT_REGULAR flag.



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

function date_to_timestamp($d) {

// Loop through dates.
for($i = 0; $i < count($d); $i++) {
// Convert to UNIX time using strtotime()
$d[$i] = strtotime($d[$i]);
}

// Sort the array numerically
sort($d, SORT_NUMERIC);

// Return the array.
return $d;

}


The function will now return an array sorting the times in ascending order.

djr33
08-22-2011, 11:43 PM
SORT_REGULAR or SORT_NUMERIC? Your description differs from the code.
What your posting looks like it would work (in fact, I don't think you need to even set a sort flag), but from the first post this sounds like a homework assignment learning to use usort(). I don't see the point in usort() either, though.

valkyriesound
08-23-2011, 06:27 AM
Thanks for your suggestions... I've tried to make some progress, but I must still be lost-it doesn't print anything.

Hard to tell where my problem is since I no longer get any errors. :)

Suggestions or hints appreciated. :o





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


//Converting dates in string form to timestamps
function date_to_timestamp($d){

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

//compare function
function cmp2($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}

//converting dates array into a array of strings
foreach($dates as $key => $value) {
return $datestring=array();
}
//Running the string array into the convert to timestamp function
date_to_timestamp($datestring);
//Setting the timestamps into an array
$timestamps=array('date_to_timestamp');
//Sorting the array of timestamps
usort($timestamps,"cmp2");


echo '<pre>';
print_r($timestamps);
echo '</pre>';

valkyriesound
08-23-2011, 06:30 AM
Yes.. I can get the strtotime function fine..

Yes, this is homework and I appreciate hints.

No, I'm not allowed to use strtotime. :(

JShor
08-23-2011, 04:13 PM
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.



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.



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.



$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.