Convert two date strings into unix timestamps and subtract them to get the timestamp difference. Finally, determine the remaining time and generate a time format similar to (2 hours, 30 minutes, 20 seconds ago)
The code is as follows: |
Copy code |
Public function gettime ($ time_s, $ time_n ){ $ Time_s = strtotime ($ time_s ); $ Time_n = strtotime ($ time_n ); $ Strtime = ''; $ Time = $ time_n-$ time_s; If ($ time> = 86400 ){ Return $ strtime = date ('Y-m-d H: I: S', $ time_s ); } If ($ time> = 3600 ){ $ Strtime. = intval ($ time/3600). 'Hour '; $ Time = $ time % 3600; } Else { $ Strtime. = ''; } If ($ time> = 60 ){ $ Strtime. = intval ($ time/60). 'Mine '; $ Time = $ time % 60; } Else { $ Strtime. = ''; } If ($ time> 0 ){ $ Strtime. = intval ($ time). 'Seconds ago '; } Else { $ Strtime = "time error "; } Return $ strtime; } |
First, determine whether the value after subtraction is greater than the number of seconds in a day, 86400 seconds. If the value is greater than the value, return the time queried by the original database.
Then, determine whether it is within one hour to one day, that is, 3600-86400 seconds. If it is within one hour, X hours is returned. After the result is obtained, the remainder of the hour is removed and % is used to obtain the remainder.
Then, determine whether it is within one minute to one hour, that is, 60 seconds to 3600 seconds. If it is within one hour, X minutes is returned. After the result is obtained, you also need to use the remainder method to remove the time in minutes, and use % to get the remainder.
Finally, determine whether it is within 1 minute, that is, 0 seconds-60 seconds. If it is within 1 minute, the system returns X minutes.
Note: The above results are all connected using. =. In this way, an overall time is obtained.