Time processing in php /**
* Convert to UNIX timestamp
*/
Function gettime ($ d ){
If (is_numeric ($ d ))
Return $ d;
Else {
If (! Is_string ($ d) return 0;
If (ereg (":", $ d )){
$ Buf = split ("+", $ d );
$ Year = split ("[-/]", $ buf [0]);
$ Hour = split (":", $ buf [1]);
If (eregi ("pm", $ buf [2])
$ Hour [0] + = 12;
Return mktime ($ hour [0], $ hour [1], $ hour [2], $ year [1], $ year [2], $ year [0]);
} Else {
$ Year = split ("[-/]", $ d );
Return mktime (0, 0, 0, $ year [1], $ year [2], $ year [0]);
}
}
}
/**
*
* DateAdd (interval, number, date)
* Returns the date with the specified interval added.
* Inetrval indicates the time interval string expression to be added, for example, minute or day
* Number is a numeric expression that represents the number of time intervals to be added.
* Date indicates the Date.
*
* Interval (time Interval string expression) can be any of the following values:
* Yyyy year
* Q Quarter
* M Month
* Y Day of year
* D Day
* W Weekday
* Ww Week of year Week
* H Hour
* N Minute points
* S Second seconds
* W, y, and d play the same role, that is, add one day to the current date, add three months to q, and add seven days to ww.
*/
Function DateAdd ($ interval, $ number, $ date ){
$ Date = gettime ($ date );
$ Date_time_array = getdate ($ date );
$ Hours = $ date_time_array ["hours"];
$ Minutes = $ date_time_array ["minutes"];
$ Seconds = $ date_time_array ["seconds"];
$ Month = $ date_time_array ["mon"];
$ Day = $ date_time_array ["mday"];
$ Year = $ date_time_array ["year"];
Switch ($ interval ){
Case "yyyy": $ year + = $ number; break;
Case "q": $ month + = ($ number * 3); break;
Case "m": $ month + = $ number; break;
Case "y ":
Case "d ":
Case "w": $ day + = $ number; break;
Case "ww": $ day + = ($ number * 7); break;
Case "h": $ hours + = $ number; break;
Case "n": $ minutes + = $ number; break;
Case "s": $ seconds + = $ number; break;
}
$ Timestamp = mktime ($ hours, $ minutes, $ seconds, $ month, $ day, $ year );
Return $ timestamp;
}
/**
* DateDiff (interval, date1, date2)
* Returns the interval between two dates.
* Intervals (time interval string expression) can be any of the following values:
* W weeks
* D days
* H Hour
* N minutes
* S seconds
*/
Function DateDiff ($ interval, $ date1, $ date2 ){
// Obtain the number of seconds between two dates
$ Timedifference = gettime ($ date2)-gettime ($ date1 );
Switch ($ interval ){
Case "w": $ retval = bcp ($ time difference, 604800); break;
Case "d": $ retval = bcp ($ timedifference, 86400); break;
Case "h": $ retval = bcp ($ time difference, 3600); break;
Case "n": $ retval = bcp ($ timedifference, 60); break;
Case "s": $ retval = $ timedifference; break;
}
Return $ retval;
}
?>
// Test example
$ D1 = "2002-01-11 ";
$ D2 = date ("Y-m-d", dateadd ("d", 15, $ d1 ));
Echo $ d1. ". datediff (" d ", $ d1, $ d2)." The day is $ d2.
";
Echo $ d1. "10 days ago is". date ("Y-m-d", dateadd ("d",-10, $ d1 ))."
";
$ D3 = date ("Y/m/d H: I: s ");
Echo "is now". $ d3. "the distance from 12:59:59 is also". datediff ("s", $ d3, "12:59:59"). "seconds.
";
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.