PHP implementation of DateDiff and DateAdd time function code sharing, datediffdateadd_php tutorial

Source: Internet
Author: User
Tags date1 diff

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 ...

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.