Home > Backend Development > PHP Tutorial > PHP simulates two date processing functions of SQL Server_PHP tutorial

PHP simulates two date processing functions of SQL Server_PHP tutorial

WBOY
Release: 2016-07-13 17:26:45
Original
782 people have browsed it

//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")); /****Simulating the dateadd function in sqlserver************  $part type: string  Value range: year, month, day, hour, min, sec  Representation: which part of the date to be added $n type: numerical representation : How much to increase, decide which part to increase based on $part. It can be a negative number. $datetime type: timestamp . Represents: the base of the increase. Return type: timestamp **************End****** ********/ 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; } /****Simulate the datediff function in sqlserver************  $part type: string  Value range: year, month, day, hour, min, sec  Represents: which part of the date to be added $date1, $date2 type : timestamp Represents: Two dates to be compared Return type: Numeric **************End* (*************/ 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; } }

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/531932.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...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template