Home  >  Article  >  Backend Development  >  How to implement a date several days after a given date_PHP Tutorial

How to implement a date several days after a given date_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:03:39813browse

In the past few days, many people suddenly asked this question, that is, how to implement the DateAdd function in VB in PHP, haha! This is a fair question.
Originally, this question was an exam question when Doufu applied for a job at Huawei, but it was implemented in C++ at the time. I didn't expect that such a big company would use such a pediatrician to take the exam:). Later I didn't go. In the past two days, I responded to the netizens of http://www.chinaspx.com - "Fortunately, I used PHP to restart
Write this function.
This function is very simple. It just adds one day to the specified time to get the newly generated date. If you want to expand it, it is also very simple.
Let’s first look at this function. First of all, we need to talk about a function in advance to determine whether it is a leap year.
function CheckRun($year){
if($year%4==0 && ($year %100!=0 || $year%400==0) )
return true;
else
return false;
}
We will 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; //Stop talking nonsense, add the date first and then say it
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))
{
//There are 29 days in February in a leap year
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("1999-12-31 11:11:11");
echo DateAdd("2000-2-29 11:11:11");
If you want to test adding a number of days, just Just add a loop, I believe everyone is an expert, this function is very simple:)

http://www.bkjia.com/PHPjc/316189.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316189.htmlTechArticleIn the past few days, many people suddenly asked this question, that is, how to implement DateAdd in VB in PHP function, haha! This is a fair question. Originally this problem was tofu to Huawei...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn