Two functions for simulating SQL Server: DateAdd (), DateDiff () _php Tutorial

Source: Internet
Author: User
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"));

/**** simulation of DATEADD functions in SQL Server *******
$part Type: string
Value range: year,month,day,hour,min,sec
Indicates: Which part of the date to increase
$n Type: Numeric
Indicate: How much to increase, according to $part decide which part to add
Can be a negative number
$datetime Type: Timestamp
Representation: Increased cardinality
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;
}

/**** simulation of DATEDIFF functions in SQL Server *******
$part Type: string
Value range: year,month,day,hour,min,sec
Indicates: Which part of the date to increase
$date 1, $date 2 type: Timestamp
Represents: two dates to compare
return type: Numeric
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
Case ' 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-$date 1;
Break
Default
return $ret;
Break
}
return $ret;
}
?>

http://www.bkjia.com/PHPjc/316193.html www.bkjia.com true http://www.bkjia.com/PHPjc/316193.html techarticle ? PHP//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)); /...

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