php判斷兩個時間的差

/***************
 * @function                判斷遠程圖片是否存在
 * @Param:
 *     $url :               地址
 * @Return:           返回結果
 ***************/
function isRealImg($url){
    $ch = curl_init();
    $timeout = 10;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

    $contents = curl_exec($ch);
    if (preg_match("/404/", $contents)){
        return false;
    }

    return true;
}

 

/***************
 * @function                計算兩個時間的差
 * @Param:
 *     $time_1 :            第一個參數
 *     $time_2 :            第二個參數
 *     $types :                類型:1--》相差天數,2--》相差小時數,3--》相差分鐘數,4--》相差秒數,,默認1;
 * @Return:     $array      返回結果數組
 ***************/
function diffTime($time_1,$time_2,$types = 1){

    if(!$time_1 || !$time_2){return false;}

    if(strtotime(date('Y-m-d H:i:s',$time_1)) != $time_1) {
        $time_1 = strtotime($time_1);
    }

    if(strtotime(date('Y-m-d H:i:s',$time_2)) != $time_2) {
        $time_2 = strtotime($time_2);
    }

    if($time_1 < $time_2){
        $temp     = $time_1;
        $time_1    = $time_2;
        $time_2       = $temp;
    }

    switch ($types) {
        case 1:
            $diff = (strtotime(date('Y-m-d',$time_1)) - strtotime(date('Y-m-d',$time_2))) / (60*60*24);
            break;
        case 2:
            $diff = (strtotime(date('Y-m-d H',$time_1).':00:00') - strtotime(date('Y-m-d H',$time_2).':00:00')) / (60*60);
            break;
        case 3:
            $diff = (strtotime(date('Y-m-d H:i',$time_1).':00') - strtotime(date('Y-m-d H:i',$time_2).':00')) / 60;
            break;
        case 4:
            $diff = $time_1 - $time_2;
            break;
    }

    return $diff;
}
相關文章
相關標籤/搜索