The date operation function in PHP is powerful. the code below is PHP's class for date addition and subtraction. it is very good. you can use it if you need it. How to use this class? See the following demo:
The code is as follows:
$ Temptime = time ();
Echo strftime ("% Hh % M % A % d % B", $ temptime );
$ Date = new DateAccount ();
$ Temptime = $ date-> DateAdd ("n", 50, $ temptime );
Echo"
";
Echo strftime ("% Hh % M % A % d % B", $ temptime );
The code is as follows:
$ Temptime = time ();
Echo strftime ("% Hh % M % A % d % B", $ temptime );
$ Date = new DateAccount ();
$ Temptime = $ date-> DateAdd ("n", 50, $ temptime );
Echo"
";
Echo strftime ("% Hh % M % A % d % B", $ temptime );
If everything goes well, you can see the following results:
15h41 Saturday 03 Jun
16h31 Saturday 03 Jun
The code is as follows:
$ Currenttime = time ();
Echo "Current time:". strftime ("% Hh % M % A % d % B", $ currenttime )."
";
$ Date = new DateAccount ();
$ Newtime = $ date-> DateAdd ("n", 50, $ currenttime );
Echo "Time plus 50 minutes:". strftime ("% Hh % M % A % d % B", $ newtime )."
";
$ Temptime = $ date-> DateDiff ("n", $ currenttime, $ newtime );
Echo "Interval between two times:". $ temptime;
The code is as follows:
$ Currenttime = time ();
Echo "Current time:". strftime ("% Hh % M % A % d % B", $ currenttime )."
";
$ Date = new DateAccount ();
$ Newtime = $ date-> DateAdd ("n", 50, $ currenttime );
Echo "Time plus 50 minutes:". strftime ("% Hh % M % A % d % B", $ newtime )."
";
$ Temptime = $ date-> 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
The code is as follows:
Class DateAccount {
Function _ construct (){
}
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;
}
Function DateDiff ($ interval, $ date1, $ date2 ){
$ 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;
}
}
?>