Php UDF for calculating the remaining time

Source: Internet
Author: User

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.

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.