Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Time remaining as a percentage

Tags:

php

time

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.

like image 669
Joel Kennedy Avatar asked Jan 20 '23 05:01

Joel Kennedy


1 Answers

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);
like image 129
Cthos Avatar answered Jan 31 '23 08:01

Cthos