我的代码有一个有趣的问题。它应该做的是锻炼一天到另一天的进度,并给出一个百分比的进度。除非我在提交时使用值超过12的日期(例如: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 . '%';
这可能是一个非常简单的解决方案,但我看不到。如有任何帮助,我们将不胜感激。
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 . '%';
}
这应该可以解决问题。