Heim  >  Artikel  >  Backend-Entwicklung  >  如何模拟SQLServer的两个日期处理函数_PHP教程

如何模拟SQLServer的两个日期处理函数_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:03:47921Durchsuche

//在PHP中处理日期非常不方便,比如求两个日期之间相差的月份?该怎么办呢? 
//文件名: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; 
} 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/630924.htmlTechArticle//在PHP中处理日期非常不方便,比如求两个日期之间相差的月份?该怎么办呢? //文件名:date.inc.php3 //在使用这两个函数前,要先将日期或...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn