从utc时间和百分比计算日期

时间:2013-06-21 11:56:05

标签: php utc date-conversion

我正在尝试使用开始日期和今天日期计算出给定日期之间的当前百分比。两者之间的持续时间通过结束日期计算。这些值将在javascript中使用。 startdate = 2013-01-08end date = 2013-07-08以及今天的实时日期。测试我试图转换我的结果,看到它返回正确的日期。代码如下:

$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);

输出

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

更新至:

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

给出输出

15634800
14219916
90.95%

我现在要做的是从startdate和return date计算90.95%。

1 个答案:

答案 0 :(得分:2)

搜索计算百分比的数学。我没跟笑话。

$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

获取日期的百分比

$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") );