Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date Progress Bar PHP

Tags:

date

php

time

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.


1 Answers

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.

like image 188
Kamil P Avatar answered Nov 29 '25 13:11

Kamil P



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!