PHP implementation of DateDiff and DateAdd time function code sharing, Datediffdateadd
Extended time functions in PHP DateDiff and DateAdd
function DateDiff ($part, $begin, $end) {$diff = Strtotime ($end)-Strtotime ($begin), switch ($part) {case "Y": $retval = BCDI V ($diff, (60 * 60 * 24 * 365)); Break;case "M": $retval = Bcdiv ($diff, (60 * 60 * 24 * 30)); Break;case "W": $retval = Bcdiv ($diff, (60 * 60 * 24 * 7)); Break;case "D": $retval = Bcdiv ($diff, (60 * 60 * 24)); Break;case "h": $retval = Bcdiv ($diff, (60 * 60)); Break;case "n": $retval = Bcdiv ($diff, 60); Break;case "s": $retval = $diff; break;} return $retval;} function DateAdd ($part, $number, $date) {$date _array = getdate (Strtotime ($date)); $hor = $date _array["hours"]; $min = $ date_array["Minutes"]; $sec = $date _array["seconds"]; $mon = $date _array["Mon"]; $day = $date _array["Mday"]; $yar = $date _ array["Year"];switch ($part) {case "Y": $yar + = $number; break;case "Q": $mon + = ($number * 3); Break;case "M": $mon + = $num ber Break;case "W": $day + = ($number * 7); Break;case "D": $day + = $number; Break;case "H": $hor + = $number; Break;case "n": $min + = $number; Break;case "s": $sec + = $number; break;} Return Date ("Y-m-d h:i:s", Mktime ($hor, $min, $sec, $mon, $day, $yar));} Function DateAdd ($part, $n, $date) {switch ($part) {case ' y ': $val = Date ("y-m-d h:i:s", Strtotime ($date. "+ $n Year")); Brea K;case "M": $val = Date ("y-m-d h:i:s", Strtotime ($date. "+ $n Month")); Break;case "W": $val = Date ("y-m-d h:i:s", Strtotime ($date. "+ $n Week")); Break;case "D": $val = Date ("y-m-d h:i:s", Strtotime ($date. "+ $n Day")); Break;case "h": $val = Date ("y-m-d h:i:s", Strtotime ($date. "+ $n Hour")); Break;case "n": $val = Date ("y-m-d h:i:s", Strtotime ($date. "+ $n Minute")); Break;case "s": $val = Date ("y-m-d h:i:s", Strtotime ($date. "+ $n Second")); break;} return $val;}
What is DateAdd and DateDiff in the SQL statement?
DateDiff is to calculate the difference between two hours, such as a few days or months, or a few years.
Select DateDiff (Day, ' 2008.9.2 ', ' 2008.10.28 ')
DateAdd is to figure out how many days to add on the basis of the known time is a few months.
Select DATEADD (Year,10,getdate ())
Select DATEADD (Month,10,getdate ())
Select DATEADD (Day,10,getdate ())
For DateDiff function problems
Syntax
DateDiff (interval, date1, date2 [, firstdayofweek[, FirstWeekOfYear]])
The syntax for the DATEDIFF function has the following parameters:
Parameter description The
interval must be selected. A string expression that is used to calculate the time interval between Date1 and Date2. See the Settings section for values.
Date1, Date2 must be selected. The date expression. The two dates used for the calculation. The
FirstDayOfWeek is optional. Constant that specifies the first day of the week. If not specified, the default is Sunday. See the Settings section for values. The
FirstWeekOfYear is optional. A constant that specifies the first week of the year. If not specified, the default is the week in which January 1 is located. See the Settings section for values. The
Setting
Interval parameter can have the following values:
Setting description
yyyy
Q quarter
M month
y number of days
D Day
W day of week
WW Week
H-hour m minutes
S seconds
http://www.bkjia.com/PHPjc/864930.html www.bkjia.com true http://www.bkjia.com/PHPjc/864930.html techarticle PHP Implementation of the DateDiff and DateAdd time function code sharing, Datediffdateadd extension PHP in the time function DateDiff and DateAdd function DateDiff ($part, $begin, $ End) {$diff = Strtotime ($end ...