Home  >  Article  >  Backend Development  >  How to implement a date several days after a specified date in PHP

How to implement a date several days after a specified date in PHP

WBOY
WBOYOriginal
2016-08-08 09:33:471250browse

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 the current time is a leap year
function CheckRun($year){
if($year%4==0 && ($year%100!=0 || $year%400==0) )
return true;
else
return false;
}
We are 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; //Stop talking nonsense, add the date first and then talk about 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 leap years
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 now
echo DateAdd("2013-12-31 11:11:11");
echo DateAdd("2014-2-29 11:11:11");

The above introduces how PHP implements the date several days after the specified date, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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