Sort an array of dates using usort() and sort() functions by the timestamp converted by mktime()

前端 未结 2 680
猫巷女王i
猫巷女王i 2021-01-26 11:59

I have an array of dates and I have to sort it by using the described functions.

Here is what I have:

$dates = array (\'10-10-2003\', \'2-17-2002\', \'2-         


        
2条回答
  •  太阳男子
    2021-01-26 12:27

    In your date_tim_timestamp function, you are essentially throwing away your date in place of the integer value.

    Try this instead:

    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]));
            $int_version = mktime(0,0,0,$arr[0],$arr[1],$arr[2]);
            $newarr[$int_version] = $f;
        }
        return $newarr;
    }
    

    Using this approach, you wont need to use usort(), just ksort()

提交回复
热议问题