Home > Article > Backend Development > How to simulate two date processing functions of SQL Server_PHP tutorial
//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;
}