date.inc.php3,類比SQLSERVER的兩個函數:dateadd(),datediff()

來源:互聯網
上載者:User
server|sqlserver|函數 //檔案名稱:date.inc.php3
//在使用這兩個函數前,要先將日期或日期時間轉換成timestamp類型。
//如:
//$today=mktime(0,0,0,date("m"),date("d"),date("Y"));

/****類比sqlserver中的dateadd函數*******
$part    類型:string
        取值範圍:year,month,day,hour,min,sec
        表示:要增加的日期的哪個部分
$n        類型:數值
        表示:要增加多少,根據$part決定增加哪個部分
            可為負數
$datetime類型:timestamp
        表示:增加的基數
返回    類型:timestamp
**************結束**************/
function dateadd($part,$n,$datetime){
    $year=date("Y",$datetime);
    $month=date("m",$datetime);
    $day=date("d",$datetime);
    $hour=date("H",$datetime);
    $min=date("i",$datetime);
    $sec=date("s",$datetime);
    $part=strtolower($part);
    $ret=0;
    switch ($part) {
        case "year":
            $year+=$n;
            break;
        case "month":
            $month+=$n;
            break;
        case "day":
            $day+=$n;
            break;
        case "hour":
            $hour+=$n;
            break;
        case "min":
            $min+=$n;
            break;
        case "sec":
            $sec+=$n;
            break;
        default:
            return $ret;
            break;
    }
$ret=mktime($hour,$min,$sec,$month,$day,$year);
    return $ret;
}

/****類比sqlserver中的datediff函數*******
$part    類型:string
        取值範圍:year,month,day,hour,min,sec
        表示:要增加的日期的哪個部分
$date1,$date2    類型:timestamp
        表示:要比較的兩個日期
返回    類型:數值
**************結束*(*************/
function datediff($part,$date1,$date2){
    //$diff=$date2-$date1;
    $year1=date("Y",$date1);
    $year2=date("Y",$date2);
    $month2=date("m",$date2);
    $month1=date("m",$date1);
    $day2=date("d",$date2);
    $day1=date("d",$date1);
    $hour2=date("d",$date2);
    $hour1=date("d",$date1);
    $min2=date("i",$date2);
    $min1=date("i",$date1);
    $sec2=date("s",$date2);
    $sec1=date("s",$date1);

    $part=strtolower($part);
    $ret=0;
    switch ($part) {
        case "year":
            $ret=$year2-$year1;
            break;
        case "month":
            $ret=($year2-$year1)*12+$month2-$month1;
            break;
        case "day":
            $ret=(mktime(0,0,0,$month2,$day2,$year2)-mktime(0,0,0,$month1,$day1,$year1))/(3600*24);
            break;
        case "hour":
            $ret=(mktime($hour2,0,0,$month2,$day2,$year2)-mktime($hour1,0,0,$month1,$day1,$year1))/3600;
            break;
        case "min":
            $ret=(mktime($hour2,$min2,0,$month2,$day2,$year2)-mktime($hour1,$min1,0,$month1,$day1,$year1))/60;
            break;
        case "sec":
            $ret=$date2-$date1;
            break;
        default:
            return $ret;
            break;
    }
    return $ret;
}



相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。