PHP calculates the number of years, months, and days of time difference.

Source: Internet
Author: User
Tags date1 getdate time and date

The effect is as follows:

The code is as follows. It must be noted that: 1. A year is defined as 360 days, and a month is 30 days. 2. In the code, 86400 = 24*60*60, represents the total number of seconds in a day. 3. Both of these time periods must be written in a format similar to. 4. All PHP programs should be promoted, you can change the parameter Get_option ('swt _ builddate') for wordpress to the time parameter to be compared.

   

The code is as follows: Copy code
<? Php
// Get detail gap of year, month and days between two different time by vfhky 20130728
$ Common = (time ()-strtotime (get_option ('swt _ builddate ')));
$ A = floor ($ common/86400/360); // An integer year
$ B = floor ($ common/86400/30)-$ a * 12; // An integer month
$ C = floor ($ common/86400)-$ a * 360-$ B * 30; // integer day
$ D = floor ($ common/86400); // total number of days
Echo $ a. "year". $ B. "month". $ c. "(total". $ d. "days )";
?>

Other methods

The code is as follows: Copy code

<? Php
Function count_days ($ a, $ B ){
$ A_dt = getdate ($ );
$ B _dt = getdate ($ B );
$ A_new = mktime (12, 0, 0, $ a_dt ['mon'], $ a_dt ['mday'], $ a_dt ['Year']);
$ B _new = mktime (12, 0, 0, $ B _dt ['mon'], $ B _dt ['mday'], $ B _dt ['Year']);
Return round (abs ($ a_new-$ B _new)/86400 );
}
// How many days is the difference between today and October 11, 2008?
$ Date1 = strtotime (time ());
$ Date1 = strtotime ('2014/1/123 ');
$ Result = count_days ($ date1, $ date2 );
Echo $ result;
?>

Example 2

The code is as follows: Copy code

<? Php
// How many days is the difference between today and September 9, 2008?
$ Date_1 = date ("Y-m-d ");
$ Date_2 = "2008-10-11 ";
$ D1 = strtotime ($ Date_1 );
$ D2 = strtotime ($ Date_2 );
$ Days = maid ($ d2-$ d1)/3600/24 );
Echo "today is different from October 11, 2008". $ Days. "day ";
?>

Summary

From the above example, we can see that mktime and strtotime are actually used. Then, after the calculated time is used for addition and subtraction, we will get the time and date we need.

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.