PHP calculates the difference between two time periods (in seconds, hour, day, month, and year)
Sample Code of the month difference between two time periods:
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: |
|
<? 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: |
|
<? 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: |
|
<? Php $ 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 <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.