Get most recent date from an array of dates

后端 未结 11 688
小鲜肉
小鲜肉 2020-11-28 11:32

I have the array of dates below

array(5) { 
    [0]=> string(19) \"2012-06-11 08:30:49\" 
    [1]=> string(19) \"2012-06-07 08:03:54\" 
    [2]=> st         


        
11条回答
  •  一向
    一向 (楼主)
    2020-11-28 11:41

    $arrayy = array(
        "2012-06-11 08:30:49","2012-06-07 08:03:54","2012-05-26 23:04:04",
        "2012-05-27 08:30:00","2012-06-08 08:30:55" 
    );
    
    function getMostRecent($array){
        $current = date("Y-m-d h:i:s");
        $diff1 = NULL;
        $recent = NULL;
        foreach($array as $date){
            if($diff = strcmp($current,$date)){
                if($diff1 == NULL){
                    $diff1 = $diff;
                    $recent = $date;
                }
                else{
                    if($diff < $diff1){
                        $diff1 = $diff;
                        $recent = $date;
                    }
                }
            }
        }
        return $recent;
    }
    

提交回复
热议问题