Php 进度条不接受任何高于12的月或日值

Php 进度条不接受任何高于12的月或日值,php,Php,我的代码有一个有趣的问题。它应该做的是计算一天到另一天的进度,并给出一个百分比的进度。除非我在提交时使用了一个值大于12的日期(例如:2015-11-16),否则无论该日期之前多长时间,每次都会给出100%。我已经附上了两个代码部分 在数据库中插入: if(count($_POST)>0) { $title = $_POST['title']; $detail = $_POST['detail']; $completion= ''.$_POST['completion'].' 9:00:0

我的代码有一个有趣的问题。它应该做的是计算一天到另一天的进度,并给出一个百分比的进度。除非我在提交时使用了一个值大于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 . '%';
从数据库显示:

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 . '%';
}
这应该可以解决问题


这应该可以解决问题。

非常感谢。然而,如果这一年超过2015年,它似乎就会崩溃@coderleo@LaughingQuoll尝试使用类而不是strotime(),因为它(DateTime)似乎是处理时间的更好方法。你可能还想调查一下。别担心。我弄错了方向。如果我把它们换过来就好了。非常感谢你。然而,如果这一年超过2015年,它似乎就会崩溃@coderleo@LaughingQuoll尝试使用类而不是strotime(),因为它(DateTime)似乎是处理时间的更好方法。你可能还想调查一下。别担心。我弄错了方向。如果我把它们换过来就好了。谢谢