PHP emulates SQL Server two date processing functions

Source: Internet
Author: User
Tags date datetime end functions numeric sql numeric value range
server| function//in PHP processing date is very inconvenient, such as the two date difference between the month? What should we do?
FileName: date.inc.php3
Before you use these two functions, you must first convert the date or datetime to the timestamp type.
Such as:
$today =mktime (0,0,0,date ("M"), Date ("D"), Date ("Y"));
/**** simulate the DATEADD function in SQL Server *******
$part Type: string
Value range: year,month,day,hour,min,sec
Which part of the date to increase
$n Type: Numeric value
said: To increase how much, according to $part decide which part to add
Can be a negative number
$datetime Type: Timestamp
Indicated: 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;
}

/**** simulate the DATEDIFF function in SQL Server *******

$part Type: string
Value range: year,month,day,hour,min,sec
Which part of the date to increase
$date 1, $date 2 type: Timestamp
Indicates: two dates to compare
Return type: Numeric 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
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;
}

}


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.