How to implement a date of a given date after several days _php
Source: Internet
Author: User
Keywords date later several how realized monthdayif this
These days suddenly a lot of people ask such a question, is how to implement in PHP in VB DateAdd function, hehe! But this is a question.
Originally this problem is tofu to Huawei applied for a test question, but it was implemented in C + +. Did not expect such a large company, unexpectedly
Then use this kind of pediatrics to test:), later I did not go, these two days should http://www.chinaspx.com of net friend-"luck, with PHP again
Wrote this function.
This function is very simple, that is, add a day to the specified time, get the new generation of the date, if you want to expand, it is very simple.
First of all, this function, first of all, to tell a function in advance to determine whether the current is a leap year function
function CheckRun ($year) {
if ($year%4==0 && ($year%100!=0 | | $year%400==0))
return true;
Else
return false;
}
We're going to have this function in the following procedure.
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; Cut the crap, add the date first.
if ($month = = ' 1 ' | | $month = = ' 3 ' | | $month = = ' 5 ' | | $month = = ' 7 ' | | $month = = ' 8 ' | | $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 add a few days, just add a loop on it, I believe that everyone is an expert, 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.