简体   繁体   中英

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.

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('ymj&\\nb\\sp;g:i:s') because it's the same with $today .

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