PHP生成GUID算法

/**
 * 创建GUID
 *
 * @return string
 */
function get_guid(){
    $tmp = md5(uniqid(md5(microtime(true)),true));
    $units = substr($tmp,0,8) . '-' . substr($tmp,8,4) . '-' . substr($tmp,12,4) . '-' . substr($tmp,16,4) . '-' . substr($tmp,20,12);
    return $units;
}

PHP计算时间日期差并检验日期格式是否正确(月龄计算)

/**
 * 校验日期格式是否正确
 * 
 * @param string $date 日期
 * @param string $formats 需要检验的格式数组
 * @return boolean
 */
function checkDateIsValid($date, $formats = array("Ymd","Y-m-d", "Y/m/d")) {
    $unixTime = strtotime($date);
    if (!$unixTime) { //strtotime转换不对,日期格式显然不对。
        return false;
    }
    //校验日期的有效性,只要满足其中一个格式就OK
    foreach ($formats as $format) {
        if (date($format, $unixTime) == $date) {
            return true;
        }
    }
    return false;
}
/**
 * 计算日期时间差
 * 
 * @param string $date_befor 日期1
 * @param string $date_after 日期2
 * @param string $diff_type 返回类型 YMDHIS
 * @return boolean | int 相差结果 失败返回false
 */
function calcDateDiff($date_befor,$date_after,$diff_type = 'm'){
    if( checkDateIsValid($date_befor) && checkDateIsValid($date_after) ){
        $date_befor = strtotime($date_befor);
        $date_after = strtotime($date_after);
        switch(strtoupper($diff_type)){
            case 'Y':
                $c['B']['Y'] = date('Y',$date_befor);
                $c['B']['M'] = date('m',$date_befor);
                $c['B']['D'] = date('d',$date_befor);
                $c['A']['Y'] = date('Y',$date_after);
                $c['A']['M'] = date('m',$date_after);
                $c['A']['D'] = date('d',$date_after);
                $c['C']['Y'] = ($c['A']['Y'] - $c['B']['Y']) * 12;
                $c['C']['M'] = ($c['A']['M'] - $c['B']['M']) * 1;
                $c['C']['D'] = ($c['A']['D'] - $c['B']['D']) >= 0 ? 0 : -1 ;
                return floor(($c['C']['Y'] + $c['C']['M'] + $c['C']['D'] )/12) ;
                break;
            case 'M':
                $c['B']['Y'] = date('Y',$date_befor);
                $c['B']['M'] = date('m',$date_befor);
                $c['B']['D'] = date('d',$date_befor);
                $c['A']['Y'] = date('Y',$date_after);
                $c['A']['M'] = date('m',$date_after);
                $c['A']['D'] = date('d',$date_after);
                $c['C']['Y'] = ($c['A']['Y'] - $c['B']['Y']) * 12;
                $c['C']['M'] = ($c['A']['M'] - $c['B']['M']) * 1;
                $c['C']['D'] = ($c['A']['D'] - $c['B']['D']) >= 0 ? 0 : -1 ;
                return $c['C']['Y'] + $c['C']['M'] + $c['C']['D'] ;
                break;
            case 'D':
                return floor(($date_after - $date_befor)/86400);
                break;
            case 'H':
                return floor(($date_after - $date_befor)/3600);
                break;
            case 'I':
                return floor(($date_after - $date_befor)/60);
                break;
            case 'S':
                return $date_after - $date_befor;
                break;
            default:
                return $date_after - $date_befor;
                break;
        }
        return true;
    }
    return false;
}

 

PHP四大排序算法

抄自:董鑫

BUBBLE 冒泡排序

代码实现:
$arr=array(1,43,54,62,21,66,32,78,36,76,39);  
function bubbleSort($arr)
{  
  $len=count($arr);
  //该层循环控制 需要冒泡的轮数
  for($i=1;$i<$len;$i++)
  { //该层循环用来控制每轮 冒出一个数 需要比较的次数
    for($k=0;$k<$len-$i;$k++)
    {
       if($arr[$k]>$arr[$k+1])
        {
            $tmp=$arr[$k+1];
            $arr[$k+1]=$arr[$k];
            $arr[$k]=$tmp;
        }
    }
  }
  return $arr;
}

SELECT 选择排序

代码实现:
$arr=array(1,43,54,62,21,66,32,78,36,76,39);  
function bubbleSort($arr)
{  
  $len=count($arr);
  //该层循环控制 需要冒泡的轮数
  for($i=1;$i<$len;$i++)
  { //该层循环用来控制每轮 冒出一个数 需要比较的次数
    for($k=0;$k<$len-$i;$k++)
    {
       if($arr[$k]>$arr[$k+1])
        {
            $tmp=$arr[$k+1];
            $arr[$k+1]=$arr[$k];
            $arr[$k]=$tmp;
        }
    }
  }
  return $arr;
}

INSERT 插入排序

function insertSort($arr) {
    $len=count($arr); 
    for($i=1, $i<$len; $i++) {
        $tmp = $arr[$i];
        //内层循环控制,比较并插入
        for($j=$i-1;$j>=0;$j--) {
            if($tmp < $arr[$j]) {
                //发现插入的元素要小,交换位置,将后边的元素与前面的元素互换
                $arr[$j+1] = $arr[$j];
                $arr[$j] = $tmp;
            } else {
                //如果碰到不需要移动的元素,由于是已经排序好是数组,则前面的就不需要再次比较了。
                break;
            }
        }
    }
    return $arr;
}

MERGE 归并排序

<?php  
$arrStoreList = array(3,2,4,1,5);  
$sort = new Merge_sort();  
$sort->stableSort($arrStoreList, function ($a, $b) {    // function ($a, $b)匿名函数  
            return $a < $b;  
});  
//静态调用方式也行  
/*Merge_sort:: stableSort($arrStoreList, function ($a, $b) { 
            return $a < $b; 
});*/  
print_r($arrStoreList);  

class Merge_sort{  

 public static function stableSort(&$array, $cmp_function = 'strcmp') {  
        //使用合并排序  
        self::mergeSort($array, $cmp_function);  
        return;  
    }  
 public static function mergeSort(&$array, $cmp_function = 'strcmp') {  
        // Arrays of size < 2 require no action.  
        if (count($array) < 2) {  
            return;  
        }  
        // Split the array in half  
        $halfway = count($array) / 2;  
        $array1 = array_slice($array, 0, $halfway);  
        $array2 = array_slice($array, $halfway);  
        // Recurse to sort the two halves  
        self::mergeSort($array1, $cmp_function);  
        self::mergeSort($array2, $cmp_function);  
        // If all of $array1 is <= all of $array2, just append them.  
//array1 与 array2 各自有序;要整体有序,需要比较array1的最后一个元素和array2的第一个元素大小  
        if (call_user_func($cmp_function, end($array1), $array2[0]) < 1) {    
            $array = array_merge($array1, $array2);  

            return;  
        }  
        // 将两个有序数组合并为一个有序数组:Merge the two sorted arrays into a single sorted array  
        $array = array();  
        $ptr1 = $ptr2 = 0;  
        while ($ptr1 < count($array1) && $ptr2 < count($array2)) {  
            if (call_user_func($cmp_function, $array1[$ptr1], $array2[$ptr2]) < 1) {  
                $array[] = $array1[$ptr1++];  
            } else {  
                $array[] = $array2[$ptr2++];  
            }  
        }  
        // Merge the remainder  
        while ($ptr1 < count($array1)) {  
            $array[] = $array1[$ptr1++];  
        }  
        while ($ptr2 < count($array2)) {  
            $array[] = $array2[$ptr2++];  
        }  
        return;  
    }  
}  
?>