Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-06-13 10:06:59
Original
832 people have browsed it

//在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; 
} 

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template