我如何找到完成百分比?

时间:2015-01-05 04:34:16

标签: php datetime

我正在开发一个计算项目完成百分比的PHP页面。例如,如果您的开始日期为2015年1月1日,结束日期为2015年3月3日,而今天为2015年2月2日,则该项目估计将完成约50%。到目前为止,我已尝试使用DateTime类和date_diff函数,但我无法将这两者分开,所以我回到了第一个。显然,我需要考虑夏令时和闰年,这样就增加了额外的复杂性。有任何想法吗?这是当前的阻止。

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();
}

5 个答案:

答案 0 :(得分:1)

MySQL有一些非常容易使用的功能,你可以在查询中收集所需的信息:

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

从那里你只需要将days_remaining除以total_days来获得百分比。 Datediff应考虑闰年和夏令时。

如果需要更精确,可以使用TIMEDIFF代替DATEDIFF,只需确保将sql时间戳转换为带有strtotime的整数。

您可能还需要设置时区:

SET time_zone = 'America/Los_Angeles';

答案 1 :(得分:1)

参考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;

答案 2 :(得分:1)

公式为:

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

作为PHP函数:

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

示例:

$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!';

答案 3 :(得分:0)

以正确的格式(YYYY-MM-DD)获取SQL输出,然后将其推送到下面的代码中:

<?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;

?>

答案 4 :(得分: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!";

描述:这计算剩余天数的百分比。 interval = $ start to $ end。计算的百分比与现在相关。