How to implement a number of days after a given date (somewhat like DateAdd in VB)
/*
Tofu production is a boutique
Http://www.asp888.net Tofu Technology Station
If reproduced please retain the full copyright information
*/
These days suddenly a lot of people ask such a question, is how to implement in PHP in VB DateAdd function, hehe! This is a question.
Originally this problem is tofu to apply to Huawei when a test question, but was implemented in C + +. Never thought of such a large company, unexpectedly
However, in this kind of pediatrics to test: Later, I did not go, these two days should http://www.chinaspx.com net friend--"luck, with PHP again
Wrote this function.
This function is very simple, is added to the specified time plus a day, get the new generated date, if you want to expand, it is also very simple.
Let's start with this function, first of all, to tell a function in advance to determine whether the current is a leap year
function Checkrun ($year) {
if ($year%4==0 && ($year%100!=0 | | | $year%400==0))
return true;
Else
return false;
}
We're going to use this function in the following program
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; Say less, add a date first
if ($month = = ' 1 ' | | $month = = ' 3 ' | | $month = ' 5 ' | | | $month = ' 7 ' | | | $month = ' 8 ' | | $month = ' | | $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))
{
Leap year February has 29 days
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, here's the test.
Echo DateAdd ("1999-12-31 11:11:11");
Echo DateAdd ("2000-2-29 11:11:11");
If you want to test the addition of several days, as long as the addition of a cycle can be, I believe we are high, this function is very simple:
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.