PHP two date handler for SQL Server simulation _php Tutorial

Source: Internet
Author: User
Working with dates in PHP is inconvenient, such as finding a month between two dates?   What should we do?   File name: DATE.INC.PHP3//Before using these two functions, convert the date or datetime to the timestamp type.   such as://$today =mktime (0,0,0,date ("M"), Date ("D"), Date ("Y")); /**** simulates the DATEADD function in SQL Server ******* $part Type: String value range: Year,month,day,hour,min,sec represents: Which part of the date to increase $n type: Numeric representation: To How much increases, depending on $part decide which part can be negative $datetime type: Timestamp: Added cardinality return type: Timestamp ************** end **************/Functio n 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; }/**** simulates the DATEDIFF function in SQL Server ******* $part Type: String value range: Year,montH,day,hour,min,sec says: Which part of the date to increase $date 1, $date 2 type: Timestamp: Two date return type to compare: Value ************** End * (************ */function DateDiff ($part, $date 1, $date 2) {//$diff = $date 2-$date 1; $year 1=date ("Y", $date 1), $year 2=date ("Y", $date 2); $month 2=date ("M", $date 2); $month 1=date ("M", $date 1); $day 2=date ("D", $date 2); $day 1=date ("D", $date 1); $hour 2=date ("D", $date 2); $hour 1=date ("D", $date 1); $min 2=date ("i", $date 2); $min 1=date ("i", $date 1); $sec 2=date ("s", $date 2); $sec 1=date ("s", $date 1); $part =strtolower ($part); $ret = 0; Switch ($part) {case ' year ': $ret = $year 2-$year 1; break, case ' month ': $ret = ($year 2-$year 1) *12+ $month 2-$month 1; break; CA Se "Day": $ret = (Mktime (0,0,0, $month 2, $day 2, $year 2)-mktime (0,0,0, $month 1, $day 1, $year 1))/(3600*24); Break Case "Hour": $ret = (mktime ($hour 2,0,0, $month 2, $day 2, $year 2)-mktime ($hour 1,0,0, $month 1, $day 1, $year 1))/3600; Break Case "min": $ret = (Mktime ($hour 2, $min 2,0, $month 2, $day 2, $year 2)-mktime ($hour 1, $min 1,0, $month 1, $day 1, $year 1))/60; Break Case "SEC": $ret = $date 2-$date1; Break Default:return $ret; Break } return $ret; } }

http://www.bkjia.com/PHPjc/531932.html www.bkjia.com true http://www.bkjia.com/PHPjc/531932.html techarticle //working with dates in PHP is very inconvenient, such as finding a month between two dates? What should we do? File name: DATE.INC.PHP3//Before using these two functions, you must first set the date or ...

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