How PHP implements dates after several days of a specified date

Source: Internet
Author: User
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 ("2013-12-31 11:11:11");
Echo DateAdd ("2014-2-29 11:11:11");

The above describes how PHP implements the date after a certain number of days of the specified date, including aspects of the content, I hope to be interested in the PHP tutorial friends helpful.

  • Contact Us

    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.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.