As I mentioned at the beginning of the article, the reason for writing this article is that I cannot find a DateDiff function similar to ASP in php. After introducing how PHP processes the date and time, let's port two common functions in ASP to PHP. The first function is DateAdd.
According to Vbscript documents, the DateAdd (interval, number, date) function is defined as "return the date with the specified time interval added ."
Inetrval indicates the time interval string expression to be added, such as minute or day. number indicates 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
The roles of w, y, and d are exactly the same, 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_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 ;}
?
We can save the above code as the dateadd. inc file, and then run the following code:
?
Include ('dateadd. Inc ');
$ Temptime = time ();
Echo strftime ("% Hh % M % A % d % B", $ temptime );
$ Temptime = DateAdd ("n", 50, $ temptime );
Echo "p ";
Echo strftime ("% Hh % M % A % d % B", $ temptime );
?
We will get:
15h41 Saturday 03 Jun
16h31 Saturday 03 Jun
Add a DateDiff function to PHP
Now that DateAdd is complete, what about DateDiff?
According to the document, the DateDiff (interval, date1, date2) function is defined as "return the interval between two dates ".
The Intervals parameter is used in the same way as that in the DateAdd function. To avoid being too complex, we decided to ignore other complex parameters in the DateDiff function in Vbscript, that is, the two optional parameter variables [firstdayofweek [, firstweekofyear] (they are used to determine the constants of the first day of a week, Sunday, Monday, and the first week of a year. In addition, intervals can only have the following five values: "w" (week), "d" (day), "h" (hour), and "n" (minute) and "s" (seconds ).
Let's see what we can come up with: the following code is what we need:
?
Function DateDiff ($ interval, $ date1, $ date2 ){
// Obtain the number of seconds between two dates
$ Timedifference = $ date2-$ 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 ;}
?
Save the above code as the datediff. inc file, and then run the following code:
?
Include ('datediff. Inc ');
Include ('dateadd. Inc ');
$ Currenttime = time ();
Echo "Current time:". strftime ("% Hh % M % A % d % B", $ currenttime). "br ";
$ Newtime = DateAdd ("n", 50, $ currenttime );
Echo "Time plus 50 minutes:". strftime ("% Hh % M % A % d % B", $ newtime). "br ";
$ Temptime = DateDiff ("n", $ currenttime, $ newtime );
Echo "Interval between two times:". $ temptime;
?
If everything goes well, you can see the following results:
Current time: 16h23 Saturday 03 Jun
Time plus 50 minutes: 17h13 Saturday 03 Jun
Interval between two times: 50
If you run PHP on a Unix machine, you must compile PHP to support high-precision BC functions. You must begin from the following address. (For details, see README. BCMATH in PHP4 ). In Windows of PHP4, you can directly use the BC high-precision function without any patching.
Now you have the date and time processing function, and the rest is how to apply it to your PHP program.
Php time Display example
Time added: 17:22:53 size: 1 KB read count: 1564
First: Simple (for learning)
$ Today = date ("Y-m-d G: I: s ");
Echo" $ Today ";
?>
Second:
/*
Format Time
*/
Function formatTime ($ time, $ type = "1 "){
Switch ($ type ){
Case 1; #2002-06-0418: 58 Tuesday
Return date ("Y. m. d H: I", $ time). "". date ("l", $ time )."";
Case 2; # Jun 2002
Return date ("m y", $ time );
Case 3; #
Return date ("Y. m. d H: I", $ time );
Case 4; #06-04 AM
Return date ("m-d A", $ time );
Case 5; #06-04
Return date ("m. d H: I", $ time );
}
} I don't know what the comparison results will be for two dates stored in string format, for example, '2017-07-12 ''2017-06-27'. what is the best method to compare these dates? In terms of size
/*
First, use the explode function to split the string.
Explode ("this quotation mark contains conditions. if it is changed to: it will use: to split the string, which can be applied to many aspects.", the string to be split later)
*/
/*
After the split string, we use the mktime function to calculate the time,
It must be noted that the time calculated by mktime is calculated in seconds,
It's easy to follow. for details, refer to the manual, which is very detailed.
*/
Echo "the result you want is $ Days ";
?>
How can I get a date after 90 days?
Time added: 18:09:53 size: 1 KB read count: 158
// Obtain the current time information
$ Y = date (Y );
$ M = date (m );
$ D = date (d );
// $ M indicates the month, $ d indicates the day, $ Y indicates the year, and "+" indicates the date before the day, "Y-m-d", only the display format. you can change it to "Y-m-d ",
$ Out_date1 = date ("Y, m, d", mktime (, 0, $ m, $ d + 7, $ Y); // One week later
$ Out_date2 = date ("Y, m, d", mktime (, 0, $ m, $ d + 14, $ Y); // Two weeks later
$ Out_date3 = date ("Y, m, d", mktime (, 0, $ m + 1, $ d, $ Y); // One month later
$ Out_date4 = date ("Y, m, d", mktime (, 0, $ m + 2, $ d, $ Y); // Two months later
$ Out_date5 = date ("Y, m, d", mktime (, 0, $ m + 3, $ d, $ Y); // Three months later
$ Out_date6 = date ("Y, m, d", mktime (, 0, $ m + 6, $ d, $ Y); // Six months later
$ Out_date7 = date ("Y, m, d", mktime (, 0, $ m, $ d, $ Y + 1); // One Year Later
Echo date ("Ymd", time () + 90*86400 );
Echo date ("Y-m-d", strtotime ("2004-10-26 + 90day "));
Date ("Y-m-d", strtotime ("+ 90 day "));
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