Question

I am trying to work out current percentage complete between given dates using start date and todays date. The duration between the two is calcuklated via end date. These values to be used in javascript. startdate = 2013-01-08 and end date = 2013-07-08 and todays realtime date. to test I am trying to convert my result and see it is returning the correct date. the code is as follows:

$duration = ( ((strtotime($model['ProjectEndDate']) - 1*86400)*1000) - ((strtotime($model['StartDATE']) - 1*86400)*1000) );
            echo $duration;
            echo "<br>";

            $burn = ((time() - 1*86400)*1000) - ((strtotime($model['StartDATE']) - 1*86400)*1000);
            echo $burn;
            echo "<br>";

            $pBurned = $burn/$duration;
            echo $pBurned*100;
            echo "<br>";

            $time = $time = strtotime( ($date_from + $pBurned) );
            echo date("Y-m-d H:i:s", $time);

The output is

1.56348E+10
14212291000
90.901648885819
1970-01-01 01:00:00

Update to:

$today = time();
            $startdate = strtotime($model['StartDATE']); //
            $enddate = strtotime($model['ProjectEndDate']); // 


            $diff_total = $enddate - $startdate;
            echo $diff_total;
            echo "<br>";

            $diff_today = $today - $startdate;
            echo $diff_today;
            echo "<br>";

            $percentage_date=round(($diff_today/$diff_total)*100,2);
            //echo $diff_today/$diff_total;
            echo $percentage_date.'%';
            exit();

gives the output

15634800
14219916
90.95%

What I want to do with this now is calculate 90.95% from startdate and return date.

Was it helpful?

Solution

search for maths on working out percentages. no joke i did.

$today = new DateTime();
$startdate = new DateTime("2013-06-16"); //$model['StartDATE']
$enddate = new DateTime("2013-06-26"); // $model['ProjectEndDate']

$diff_total = $enddate->diff($startdate)->format("%a");    
$diff_today = $today->diff($startdate)->format("%a");    
$percentage_date=round(($diff_today/$diff_total)*100,2);

echo $percentage_date.'%'; // from today

get percentage of a date

$percentage_to_get = 45;
$percentage_of_days =  floor(($diff_total/100*)$percentage_to_get);
echo date('Y-m-d', strtotime( $startdate->format('Y-m-d') ." + $percentage_of_days day") );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top