我的代码有一个有趣的问题。它应该做什么来推动从一天到另一天的进展,并以百分之一的方式取得进展。除非我在提交时使用价值超过12的日子(EG:2015-11- 16 ),否则无论多长时间,它都会给出100%的时间。我附上了两个代码部分。
插入数据库:
if(count($_POST)>0) {
$title = $_POST['title'];
$detail = $_POST['detail'];
$completion= ''.$_POST['completion'].' 9:00:00';
$progress = date('Y-m-j h:i:s');
// Create connection
$conn = new mysqli($host, $mysql_user, $mysql_pass, $db);
// Check connection
if ($conn->connect_error) {
die("Connection failed: ". $conn->connect_error);
}
mysqli_query($conn, "INSERT INTO `tasks` (`id`, `taskname`, `details`, `completion`, `startdate`) VALUES ('', '$title', '$detail', '$completion', '$progress')");
echo $sql;
} else {
echo "";
}
从数据库显示:
while($row = $result->fetch_assoc()) {
$date1 = strtotime($row['completion']);
$date2 = strtotime($row['startdate']);
$today = time();
$dateDiff = $date2 - $date1;
$dateDiffForToday = $today - $date1;
$percentage = $dateDiffForToday / $dateDiff * 100;
$percentageRounded = round($percentage);
echo $percentageRounded . '%';
这可能是一个简单的解决方案,但我看不到一个。任何帮助将不胜感激。
答案 0 :(得分:1)
while($row = $result->fetch_assoc()) {
$date1 = strtotime($row['completion']);
$date2 = strtotime($row['startdate']);
$today = time();
$dateDiff = $date2 - $date1;
$dateDiffForToday = $today - $date1;
$percentage = ($dateDiffForToday / $dateDiff) * 100;
$percentageRounded = round($percentage);
echo $percentageRounded . '%';
}
那应该解决它。