设为首页   加入最爱           最新更新:全方位英文文章资料库
 
资料坊搜索: 标题 全文     
模拟SQLSERVER的两个函数:dateadd(),datediff()
   首页>>YesizeCOM>>电脑技能坊>>PHP>>正文       
 
广告招租,e-mail:yesize@hotmail.com

广告招租,e-mail:yesize@hotmail.com


//文件名: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;
}



上一篇:怎样上传文件并存入数据库 下一篇:dateadd(),datediff()两个函数的例程

版权说明:作品来源于网上,版权归作者所有,如果无意中侵犯了您的版权,请来信告知,本站将在3个工作日内删除。yesize@hotmail.com
  热门小游戏分类 
射击小游戏 连连看小游戏
台球小游戏 成人小游戏
CS小游戏 化妆小游戏
赛车小游戏 休闲小游戏
篮球小游戏 换装小游戏
儿童小游戏 牛牛小游戏
麻将小游戏 冒险小游戏
美媚小游戏 益智小游戏
体育小游戏  
Society Recreation & Sports
Travel & Leisure Communications
Vehicles Computers
Fashion Arts & Entertainment
Reference & Education Disease & Illness
Writing & Speaking Politics
Product Reviews Food & Beverage
Finance Internet Business
Self Improvement Home & Family
Health & Fitness Business
最新更新内容
警惕“公务员报考热”背
公务员考试不是丈母娘考
一名新公务员的自白:我
公务员考脑筋急转弯与挂
北努斗:2006行政能力测
大讨论:06行测BT题目大
过来人回忆:当年,我也
一家之言:公务员热折射
公务员考试申论要是有正
数十万大学生走进公务员
      粤ICP备05005424         Copyright ©2000 - 2004 Yesize.COM