How to Implement date after several days of a given date (a bit similar to dateadd in VB)

Source: Internet
Author: User
Tags php source code


Introduction: This is a detailed page on how to implement the date after several days of a given date (somewhat similar to dateadd in VB). It introduces the knowledge, skills, and experience related to PhP, and some PHP source code.



Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 327670 'rolling = 'no'>


How to Implement date after several days of a given date (a bit similar to dateadd in VB)
/*
Tofu is made of excellent products
Http://www.asp888.net bean curd technology station
If reprinted, Please retain the complete copyright information
*/
These days, many people suddenly asked this question: how to implement the dateadd function in VB in PHP! This is a question.
This was originally an exam question when tofu went to apply for a job, but it was implemented in C ++. Without thinking of such a big company
However, using this pediatrics to take the exam :), then I did not go, the two days should be http://www.chinaspx.com of netizens-"Luck, with PHP again
Write this function.
This function is very simple, that is, adding a day to the specified time to get the new date. If you want to extend it, it is very easy.
Next, let's take a look at this function. First, let's talk about a function in advance to determine whether it is a leap year function.
Function checkrun ($ year ){
If ($ year % 4 = 0 & ($ year % 100! = 0 | $ year % 400 = 0 ))
Return true;
Else
Return false;
}
We want Program Used in this function
Function dateadd ($ date ){
$ Parts = explode ('', $ date );
$ Date = $ parts [0];
$ Time = $ parts [1];
$ Ymd = explode ('-', $ date );
$ HMS = explode (':', $ time );
$ Year = $ ymd [0];
$ Month = $ ymd [1];
$ Day = $ ymd [2];
$ Hour = $ HMS [0];
$ Minute = $ HMS [1];
$ Second = $ HMS [2];
$ Day = $ day + 1; // you can add the date first.
If ($ month = '1' | $ month = '3' | $ month = '5' | $ month = '7' | $ month = '8' | $ month = '10' | $ month = '12 ')
If ($ day = 32)
{
$ Day = '1 ';
$ Month ++;
}
If ($ month = '4' | $ month = '6' | $ month = '9' | $ month = '11 ')
If ($ day = 31)
{
$ Day = '1 ';
$ Month ++;
}
If ($ month = '2 ')
If (checkrun ($ year ))
{
// 29 days for January 1, February
If ($ day = 30)
{
$ Day = 1;
$ Month ++;
}
}
Else
{
// Not a leap year
If ($ day = 29)
{
$ Day = 1;
$ Month ++;
}
}
If ($ month = 13)
{
$ Month = 1;
$ Year ++;
}
Return $ year. "-". $ month. "-". $ Day;
}
Okay. Let's test it.
Echo dateadd ("11:11:11 ");
Echo dateadd ("11:11:11 ");
If you want to test the function for several days, you only need to add a loop. I believe everyone is a senior. This function is very simple :)


"How to Implement date after several days of a given date (somewhat similar to dateadd in VB)"



Love J2EE follow Java Michael Jackson video station JSON online tools



Http://biancheng.dnbcw.info/php/327670.html pageno: 12.


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.