As I said at the beginning of the article-the reason for writing this article is because I can't find an ASP-like DateDiff function in PHP. After introducing how PHP handles the date and time, let's port the two commonly used functions in ASP to PHP. The first function is a dateadd.
According to VBScript's documentation, the DATEADD (interval,number,date) function is defined as "returns the date that the specified time interval was added. ”
Inetrval is a string expression that represents the interval to be added, such as minutes or days; number is a numeric expression that represents the number of time intervals to add;
The Interval (time interval string expression) can be any of the following values:
YYYY year
Q Quarter Quarter
M month
Y day of year number of years
D Day
W weekday days of the week
WW Week of Year Week
H Hour Hours
N Minute min
s second s
The functions of W, Y and D are exactly the same, that is, add one day to the current date, Q plus 3 months, WW plus 7 days.
?
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); 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 a 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", $temptime);
echo "P";
Echo strftime ("%hh%m%A%d%b", $temptime);
?
We will get:
15h41 Saturday June
16H31 Saturday June
Add the DateDiff function to PHP
Now that DateAdd is done, what about DateDiff?
According to the document, the DateDiff (INTERVAL,DATE1,DATE2) function is defined as "returns the time interval between two dates."
The use of the intervals parameter is the same as in the DateAdd function. To avoid overly complex considerations, we decided to ignore other complex parameters in the DateDiff function in VBScript, that is, its two optional parameter variables [firstdayofweek[, FirstWeekOfYear]] (They are used to determine whether the first day of the week is a constant of Sunday or Monday and the first week of the year.) And we only allow intervals to have the following five values: "W" (week), "D" (Day), "H" (hours), "n" (minutes), and "s" (seconds).
Let's see how we can come up with: The following code is what we need:
?
Function DateDiff ($interval, $date 1, $date 2) {
Get the number of seconds between two dates
$timedifference = $date 2-$date 1;
Switch ($interval) {
Case "W": $retval = Bcdiv ($timedifference, 604800); Break
Case "D": $retval = Bcdiv ($timedifference, 86400); Break
Case "h": $retval = Bcdiv ($timedifference, 3600); Break
Case "n": $retval = Bcdiv ($timedifference, 60); Break
Case "s": $retval = $timedifference; Break
}
return $retval;}
?
Save the above code as a 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", $currenttime);
echo "Time plus minutes:". Strftime ("%hh%m%A%d%b", $newtime). " BR ";
$temptime = DateDiff ("n", $currenttime, $newtime);
echo "Interval between:" $temptime;
?
If all goes well, you can see the following results:
Current TIME:16H23 Saturday June
Time plus minutes:17h13 Saturday June
Interval between, times:50
If you run PHP on a UNIX machine, you must compile PHP to support the BC high-precision function. You must download the BC Library from the following address http://www.php.net/extra/number4.tar.gz, then unzip it to the root of the PHP4, recompile PHP, and compile with the--enable-bcmath option. (see PHP4 for details Readme.bcmath). The PHP4 version of Windows does not require any patching to directly use BC high-precision functions.
Now that you've got the function to handle the date and time, the rest is how to apply it to your PHP program.
PHP Time Display Example
The second type:
/*
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; #June 2002
Return Date ("M Y", $time);
Case 3; #2002-06-04 18:58
Return Date ("Y.m.d h:i", $time);
Case 4; #06 -04 AM
Return Date ("M-d A", $time);
Case 5; #06-04 18:58
Return Date ("M.D h:i", $time);
}
I don't know what will happen to the comparison of two dates stored in a string format, such as ' 2007-07-12′ ' 2007-06-27′, what's the best way to compare these dates? On the size of
Leehao July 17th, 2006 1:33 AM
/* Date Comparison */
$Date _1= "2007-07-12";
$Date _2= "2007-06-27";
/*
First, use the Explode function to split the string
Explode ("The quotation marks are conditional, change to: will be used: to split the string, can be applied to many aspects of the", after the string to be split)
*/
/*
After splitting the string, we calculate the time by using the Mktime function,
It must be explained that the time calculated by mktime is counted in seconds.
The back is simple, you can read the manual, the details are very
*/
echo "You want the result is $Days days Oh";
?>
How do I get 90 days after the date
Join time: 2004-12-18 18:09:53 Size: 1 KB Hits: 158
Get today's time information
$Y =date (Y);
$m =date (m);
$d =date (d);
$m for the month, $d for the day, $Y for the year, "+" after the sign, with "-" can get how many days before the date, "Y year M D Day", just show the format, you can change to "y-m-d",
$out _date1=date ("Y year M D Day", Mktime (0,0,0, $m, $d +7, $Y));//a week later
$out _date2=date ("Y year M D Day", Mktime (0,0,0, $m, $d +14, $Y));//Two weeks later
$out _date3=date ("Y year M D Day", Mktime (0,0,0, $m +1, $d, $Y));//One months later
$out _date4=date ("Y year M D Day", Mktime (0,0,0, $m +2, $d, $Y));//Two months later
$out _date5=date ("Y year M D Day", Mktime (0,0,0, $m +3, $d, $Y));//Three months later
$out _date6=date ("Y year M D Day", Mktime (0,0,0, $m +6, $d, $Y));//Six months later
$out _date7=date ("Y year M D Day", Mktime (0,0,0, $m, $d, $Y + 1));//A 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.