Home  >  Article  >  Backend Development  >  How to simulate two date processing functions of SQL Server_PHP tutorial

How to simulate two date processing functions of SQL Server_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:03:47961browse

//It is very inconvenient to process dates in PHP. For example, find the month difference between two dates? What to do?
//File name: date.inc.php3 
//Before using these two functions, you must first convert the date or date time into the timestamp type.
//For example:
//$today=mktime(0,0,0,date("m"),date("d"),date("Y"));

/****Simulate the dateadd function in sqlserver************
$part type: string 
Value range: year, month, day, hour, min, sec 
Represents: which part of the date to be added
$n type: numeric value
Indicates: How much to add, decide which part to add based on $part
Can be negative
$datetime type: timestamp 
Represents: increasing base
Return type: timestamp 
**************Finish**************/ 
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//It is very inconvenient to process dates in PHP, such as finding the month difference between two dates? What to do? //File name: date.inc.php3 //Before using these two functions, you must first change the date or...
Statement:
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