# PHP calculates the difference between two time periods (in seconds, days, months, and years)

Source: Internet
Author: User
Tags date1
PHP calculates the difference between two time periods (in seconds, days, months, and years). example code:

The code is as follows:
\$ Yourdate = "2012-10-20 ";
\$ Yourdate_unix = strtotime (\$ yourdate );
Echo (date ("Y", \$ yourdate_unix)-date ("Y") * 12 + (date ("m", \$ yourdate_unix) -date ("m "));

Example 1

The code is as follows:
/*
* Calculate the month difference of two time periods.
* @ Param \$ st start time \$ et end time (timestamp format)
* @ Return \$ difference value returned by total
*/
Function getMonthNum (\$ st, \$ et)
{
\$ S_m = date ('N', \$ st );
\$ E_m = date ('N', \$ et );
\$ S_y = date ('Y', \$ st );
\$ E_y = date ('Y', \$ et );
\$ Total = 13-\$ s_m + (\$ e_y-\$ s_y-1) * 12 + \$ e_m; // calculates the month difference.
Return \$ total;
}

Example 2

The code is as follows:
\$ One = strtotime ('2017-05-08 07:02:40 '); // start time stamp
\$ Tow = strtotime ('2017-12-25 00:00:00 '); // end time stamp
\$ Cle = \$ tow-\$ one; // Get the timestamp difference

/* This is just a prompt
Echo ceil (\$ cle/60); // The total number of minutes
Echo ceil (\$ cle/3600); // calculates the total number of hours
Echo ceil (\$ cle/3600/24); // returns the total number of days
*/
/* Ceil () function, which is an integer in the next method */
\$ D = cell (\$ cle/3600/24 );
\$ H = cell (\$ cle % (3600*24)/3600); // % obtain the remainder
\$ M = cell (\$ cle % (3600*24)/60 );

Echo "two time differences \$ d days \$ h hours \$ m points"
?>

Example 3

The code is as follows:

/*
*
* (Www.111cn.net) function: calculate two dates in YYYY-MM-DD format, a few days different
*
*/
Function getChaBetweenTwoDate (\$ date1, \$ date2 ){

\$ Date_List_a1 = explode ("-", \$ date1 );
\$ Date_List_a2 = explode ("-", \$ date2 );

\$ D1 = mktime (0, 0, 0, \$ Date_List_a1 [1], \$ Date_List_a1 [2], \$ Date_List_a1 [0]);

\$ D2 = mktime (0, 0, 0, \$ Date_List_a2 [1], \$ Date_List_a2 [2], \$ Date_List_a2 [0]);

\$ Days = round (\$ d1-\$ d2)/3600/24 );

Return \$ Days;
}

Echo getChaBetweenTwoDate ('2017-08-11 ', '2017-08-16 ');
Echo"
";
Echo getChaBetweenTwoDate ('2017-08-16 ', '2017-08-11 ');
?>

Example 4

The code is as follows:
\$ Startdate = "11:40:00 ″;
\$ Enddate = "2012-12-12 11:45:09 ″;
\$ Date = floor (strtotime (\$ enddate)-strtotime (\$ startdate)/86400 );
\$ Hour = floor (strtotime (\$ enddate)-strtotime (\$ startdate) % 86400/3600 );
\$ Minute = floor (strtotime (\$ enddate)-strtotime (\$ startdate) % 86400/60 );
\$ Second = floor (strtotime (\$ enddate)-strtotime (\$ startdate) % 86400% 60 );
Echo \$ date. "day
";
Echo \$ hour. "hour
";
Echo \$ minute. "minutes
";
Echo \$ second. "seconds
";
?>

Example 4: One of my favorite computing tasks can be calculated as hours and seconds. of course, you still need to perform the calculation based on your needs.
From: http://www.111cn.net/phper/php-cy/66323.htm
Related Keywords:

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.