Results 1 to 7 of 7

Thread: USORT on timestamps

  1. #1
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question USORT on timestamps

    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!




    PHP Code:
    $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;
    }
      
    $timestamps=array("date_to_timestamp");
      
    usort($timestamps,"cmp2"); 

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    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.

    PHP Code:
    $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($dSORT_NUMERIC);

    // Return the array.
    return $d;


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

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Red face Progress?

    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.



    PHP Code:
    $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;
    }

    //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>'

  6. #6
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Yes.. I can get the strtotime function fine..

    Yes, this is homework and I appreciate hints.

    No, I'm not allowed to use strtotime.

  7. #7
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    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($newarrmktime(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;

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

  8. The Following User Says Thank You to JShor For This Useful Post:

    valkyriesound (08-24-2011)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •