user3579312
user3579312

Reputation:

Date Progress Bar PHP

I currently have this code:

$date1 = strtotime($row["progress"]);
$date2 = strtotime($time);
$today = time();
$num = $today - $date1;
$den = $date2 - $date1;
$percentage = ($today - $date1) / ($date2 - $date1) * 100;
echo "Current Completion Status:";
echo $percentage;

What it is supposed to do is come up with a progress status from today until the completion date. Which I will then use to create a progress bar.

However at the moment it shows 0 percent. I am using these test values for the progress row. This is 2015-11-17 12:00:00 and the current date in that format. So I can create the percentage. As stated it displays 0 as the result. How can I fix this to make it display percent completed.

Upvotes: 3

Views: 2168

Answers (1)

Kamil P
Kamil P

Reputation: 784

Try this code and adapt it to your script:

$date = date('y-m-j&\nb\sp;g:i:s');

$date1 = strtotime('2015-03-12 00:00:00');
$date2 = strtotime('2015-12-17 00:00:00');
$today = time();

$dateDiff = $date2 - $date1;
$dateDiffForToday = $today - $date1;

$percentage = $dateDiffForToday / $dateDiff * 100;
$percentageRounded = round($percentage);

echo $percentageRounded . '%';

$date2 shouldn't base on date('y-m-j&\nb\sp;g:i:s') because it's the same with $today.

Upvotes: 2

Related Questions