Home > Backend Development > PHP Tutorial > How to promote the great development and prosperity of socialist culture. How to achieve a date several days after a given date.

How to promote the great development and prosperity of socialist culture. How to achieve a date several days after a given date.

WBOY
Release: 2016-07-29 08:34:04
Original
1054 people have browsed it

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, netizens from http://www.chinaspx.com - "lucky", I re-wrote it in PHP. 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 take a look at this function first. First, 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 talk about it
if($m || $m || $m || $ m || $m || $m || $m>if($day==32)
{
$day='1';
$month++;
}
if($m || $m || $ m || $m>if($day==31)
{
$day='1';
$month++;
}
if($m>if(CheckRun($year))
{
//Leap year There are 29 days in February
if($day==30)
{
$day=1;
$month++;
}
}
else
{
//It is not a leap year
if($day==29)
{
$day=1;
$month++;
}
}
if($m>{
$m>$year++;
}
return $year . "-" . $month . "-" . $day;
}
Okay, let’s test it now
echo DateAdd("1999-12-31 11:11:11");
echo DateAdd("2000-2-29 11:11:11");
If you want to test adding After a few days, you just need to add a loop. I believe everyone is an expert. This function is very simple:)

The above introduces how to promote the great development and prosperity of socialist culture and how to achieve a date several days after a given date, including how to promote the great development and prosperity of socialist culture. I hope friends who are interested in PHP tutorials will be able to learn from it. help.

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template