Sample Code of the month difference between two time periods:
| The code is as follows: |
Copy code |
|
$ 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: |
Copy code |
|
/*
* 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: |
Copy code |
|
<? Php
$ 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: |
Copy code |
|
<? PHP
/*
*
* Function: calculate two dates in the 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 "<br> ";
Echo getChaBetweenTwoDate ('2017-08-16 ', '2017-08-11 ');
?> |
Example 4
| The code is as follows: |
Copy code |
|
<? Php
$ Startdate = "11:40:00 & Prime ;;
$ Enddate = "11:45:09 & Prime ;;
$ 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 <br> ";
Echo $ hour. "hour <br> ";
Echo $ minute. "minute <br> ";
Echo $ second. "seconds <br> ";
?> |
Example 4 is my favorite one that can be calculated to days, hours, and seconds. Of course, you still need to use it as needed.