简体   繁体   中英

PHP Time remaining as a percentage

I am creating a system maintenance page which shows when system maintenance is being performed. There is a progress bar on the page which shows how much of the maintenance has been completed. I am trying to automate the progress bar by calculating the percentage/time remaining of a system maintenance window, but am having trouble.

There are three different times stored in the database, the start time, the end time, and then there is the current time. I need to be able to work out the remaining time of a maintenance job and show it in a progress bar, going from 1% to 100%. The script should be able to calculate how much time has elapsed between the start time and end time.

I originally tried calculating the percentage between two times (the current time and the end time) but that wouldn't work as there needs to be three factors in the equation - the start time, the end time, and the current time.

Any help on this would be appreciated.

I'll assume you're storing the start and end times as a unixtimestamp.

Essentially all you need to do is figure out the percentage of the seconds elapsed versus the total seconds.

So something like:

$total_secs = $end_time - $start_time;

$elapsed_secs = time() - $start_time;

$percent = round(($elapsed_secs/$total_secs)*100);

To get the percent completed would be like this:

$completed = (($current - $start) / ($end - $start)) * 100;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM