2

I'm working on a PHP page that calculates the percentage of completion of a project. For example, if you had a start date of January 1st, 2015, and an end date of March 3rd, 2015, and today was February 2nd, 2015, the project would be estimated to be about 50% done. So far I've attempted using the DateTime class and the date_diff function, but I couldn't divide the two, so I'm back at square one. Obviously I need to take Daylight Saving and leap years into account, so that adds an additional layer of complexity to the matter. Any ideas? Here the current block.

try {
    $dbh = new PDO('mysql:host=localhost; dbname=jkaufman_hartmanbaldwin', $username, $password, array(
        PDO::MYSQL_ATTR_SSL_KEY => '../php_include/codekaufman_com.key',
        PDO::MYSQL_ATTR_SSL_CERT => '../php_include/codekaufman_com.crt',
        PDO::MYSQL_ATTR_SSL_CA => '../php_include/codekaufman_com.ca_bundle'
    ));
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $projectName = $_GET['project'];

    $sth = $dbh->prepare('SELECT start, end FROM projects WHERE name = ?');
    $sth->execute([$projectName]);

    if($sth->rowCount()) {
        $row = $sth->fetchAll(PDO::FETCH_ASSOC);

        date_default_timezone_set('America/Los_Angeles');

        $date = strtotime($row[0]['start']);
        $start = date('m/d/Y', $date);
        echo $start;

        $date = strtotime($row[0]['end']);
        $end = date('m/d/Y', $date);
        echo " " . $end;

        $today = date('m/d/y');

        echo $end - $start;
    }
} catch(PDOException $e) {
    echo $e->getMessage();
}
Jeffrey
  • 1,271
  • 2
  • 15
  • 31

5 Answers5

1

MySQL has some pretty easy to use functions for this sort of thing, you can just gather the info you need in your query:

SELECT start, end, DATEDIFF(end, start) as total_days, DATEDIFF(end, NOW()) as days_remaining
    FROM projects WHERE name = ?

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff

From there you just need to divide days_remaining by total_days to get the percentage. Datediff should take leap years and DST into account.

You can use TIMEDIFF in place of DATEDIFF if you need to be more precise, just make sure to convert the sql timestamps to integers with strtotime.

You may also need to set the timezone:

SET time_zone = 'America/Los_Angeles';
Mathew Tinsley
  • 6,805
  • 2
  • 27
  • 37
1

With reference to How to Minus two dates in php:

$start = new DateTime($row[0]['start']);
$end = new DateTime($row[0]['end']);
$today = new DateTime();

$total = $start->diff($end);
$current = $start->diff($today);
$completion = $current->days / $total->days;
Community
  • 1
  • 1
cbreezier
  • 1,188
  • 1
  • 9
  • 17
1

The formula is:

percentage = (date - start) / (end - start) * 100

As a PHP function:

function date_progress($start, $end, $date = null) {
    $date = $date ?: time();
    return (($date - $start) / ($end - $start)) * 100;
}

Example:

$start   = strtotime("January 1st 2015");
$end     = strtotime("March 3rd 2015");
$date    = strtotime("February 2nd 2015");
$percent = date_progress($start, $end, $date);

// "You are 52.46% there!"
echo 'You are ', round($percent, 2), '% there!';
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
0

Get your SQL output in the right format (YYYY-MM-DD) and then shove it into the code below:

<?php

$startDate = date_create('2015-01-01');
$endDate = date_create('2015-01-30');
$currentDate = date_create('2015-01-08');

$totalTime = date_diff($endDate, $startDate); 
$elapsedTime = date_diff($currentDate, $startDate);

$totalTimeDays = $totalTime->format("%d");
$elapsedTimeDays = $elapsedTime->format("%d");

echo "Total project time = " . $totalTimeDays . "<br/>";
echo "Elapsed project time = " . $elapsedTimeDays  . "<br/>";
echo "Percent of project complete = " . ($elapsedTimeDays / $totalTimeDays) * 100.0;

?>
Kerry Kobashi
  • 806
  • 6
  • 9
0
$start = new DateTime("<YOUR START DATE>");  // example input "2014/06/30"
$end= new DateTime("<YOUR END DATE>");
$now = new DateTime();

$intervalOBJ = $start->diff($end);
$totalDaysOfProject = $intervalOBJ->format('%a'); 
$intervalOBJ_2 = $now->diff($end);
$daysRemaining = $intervalOBJ_2->format('%a'); 
$completedPercentage = round(($daysRemaining/$totalDaysOfProject)*100);

echo $completedPercentage . "% of this project has been completed!";

Description: This calculates the percentage of days remaining. interval = $start to $end. Calculated percentage is in relation to $now.

ZenStein
  • 151
  • 7