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;
精彩评论