PHP date addition and subtraction class, very good _ PHP Tutorial

Source: Internet
Author: User
Tags date1
PHP date addition and subtraction class, very good. How to use this class? See the following demo: Copy the code: $ temptimetime (); echostrftime (% Hh % M % A % d % B, $ temptime); $ datenewDateAccount (); $ How does tempti 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;
}
}
?>

Why? See the following demo: Code: $ temptime = time (); echo strftime ("% Hh % M % A % d % B", $ temptime ); $ date = new DateAccount (); $ tempti...

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.