我正在使用PHP
我不知道这是否可行,但我有一个开始日期和结束日期,我想从这两个日期获得完成时间的百分比。
所以如果它需要采取开始结束时间和当前时间和日期并重新开始
35%完成等。
2015年11月2日14:05:15 2015年11月2日18:05:15
感谢任何提示或指导是一个很大的帮助。
我一直试图使用它,但它不起作用。
<?php
$startDate = date_create('$start');
$endDate = date_create('$end');
$currentDate = date_create('$date');
$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;
?>
答案 0 :(得分:0)
您应该删除前3行的引号
$startDate = date_create($start);
$endDate = date_create($end);
$currentDate = date_create($date);
您使用的格式错误=&gt;使用%a 计算所有天数
- 完整示例编辑 -
<?php
// Note that i prefer the use of true object notation
$start = '2010-01-01';
$end = '2010-10-01';
$date = '2010-05-01';
$startDate = new DateTime($start);
$endDate = new DateTime($end);
$currentDate = new DateTime($date);
$totalTime = $endDate->diff($startDate);
$elapsedTime = $currentDate->diff($startDate);
$totalTimeDays = $totalTime->format('%a'); // the change is here
$elapsedTimeDays = $elapsedTime->format('%a'); // the change is here
//var_dump($totalTime, $elapsedTime);
echo "Total project time = " . $totalTimeDays . "<br/>";
echo "Elapsed project time = " . $elapsedTimeDays . "<br/>";
echo "Percent of project complete = " . ($elapsedTimeDays / $totalTimeDays) * 100.0;
?>
- 编辑2 --- 您的500错误是PHP除以0错误,因为$ elapsedTime-&gt;格式('%a');返回0(或NULL)
- 编辑3 -
$start = '2015-11-02 14:05:15';
$end = '2015-11-02 18:05:15';
$date = '2015-11-02 16:12:15';
$startDate = new DateTime($start);
$endDate = new DateTime($end);
$currentDate = new DateTime($date);
$totalTime = $endDate->getTimestamp() - $startDate->getTimestamp();
$elapsedTime = $currentDate->getTimestamp() - $startDate->getTimestamp();
echo "Total project time = " . $totalTime . "<br/>";
echo "Elapsed project time = " . $elapsedTime . "<br/>";
echo "Percent of project complete = " . ($elapsedTime / $totalTime) * 100.0;