PHP用相同的值但不同的方式给出不同的结果

3
这是我的代码:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/main.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link href='http://fonts.googleapis.com/css?family=Lato' rel='stylesheet' type='text/css'>
 <meta name="viewport" content="width=device-width, initial-scale=1"><title>Day Calculator V 2.0</title>
</head>
<body>
<h1><p class="text-center"><strong>Esta es una calculadora de dias, funciona seleccionando de que mes a que mes quieres calcular los dias. </strong></p></h1>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method=post>
<div class="center-block text-center nofloat">
    <input type=radio id="rad1" class="" name="tipfech" onclick="var divOne = document.getElementById('1');
    divOne.style.visibility='hidden'" value="actual" checked> Usar fecha actual<br>
</div>
<div class="center-block text-center nofloat">
  <input type=radio id="rad2" name="tipfech" onclick="var divOne = document.getElementById('1');
divOne.style.visibility='visible'" value="fecha"> Seleccionar fecha </div>
  <div id="1" class="text-center">
    <script>
    var divOne = document.getElementById('1');
divOne.style.visibility='hidden';
</script>
    Del 
    <input class="formachica form-control center-block" type="date" name="date1">
    </div>
  <div class="text-center">
    Al
    </div>
    <input class="formachica form-control center-block" type="date" name="date2">
    <br/>
<input type=submit class="center-block btn btn-primary" value="Submit"/>
</form>
<br/>
</div>
<div id="3" class="text-center">
<?php
$tipfech = $_POST[tipfech];
$da1 = $_POST[date1];
$da2 = $_POST[date2];
if ($tipfech == "actual"){
    $yr1 = date("Y");
    $month = date("m");
    $day1 = date("d");
    $date1 = new DateTime("$yr1-$month-$day1");
    }
$date1 = new DateTime("$da1");
$date2 = new DateTime("$da2");
$interval = $date1->diff($date2);
echo "Son " . $interval->y . " años, " . $interval->m." meses, ".$interval->d." dias "; 

// shows the total amount of days (not divided into years, months and days like above)
echo "<br/>"."Son " . $interval->days . " dias ";
?>
</div>
</body>
</html>

我的“fecha actual”是当前日期,所以当我选择该选项时,例如在02/08/2015(dd-mm-yy)选择第二个日期为04/08/2015时,它返回一天,但如果我手动选择第一个日期(相同的02/08/2015),则返回两天。
示例: http://monopolo11.ninja/tests/imgs/ (照片在那里,因为我的声誉低无法将其放入文章中)
为什么会这样呢?
示例1:Example 1 示例2:Example 2

1
精通图像裁剪技巧... - Marcin Orlowski
我需要整个屏幕,这样你就可以看到日期:D,考虑到这一点,有什么建议吗? - monopolo11
修复了,我认为这样更好了。 - monopolo11
2个回答

2
您总是覆盖掉了$date1变量。
if ($tipfech == "actual"){
    $yr1 = date("Y");
    $month = date("m");
    $day1 = date("d");
    $date1 = new DateTime("$yr1-$month-$day1");
}
$date1 = new DateTime("$da1");

当您没有手动填写开始日期时,new DateTime 将使用参数 NULL 创建。它返回当前日期,但时间是实际的。
这会导致计算天数向下取整(因为少于48小时)。

在构建 DateTime 时,为什么要引用 $da1 - Marcin Orlowski
@MarcinOrlowski,我只是复制了OP的代码。这是关键部分 :-) - Luca Rainone

0

多亏了chumkiu,我解决了这个问题。 我添加了以下内容:

  if ($tipfech !== "actual"){
    $date1 = new DateTime("$da1");
    }

为什么在构造 DateTime 时要引用 $da1 - Marcin Orlowski
@MarcinOrlowski 是我发现的一种可行的方法,有没有改进的方法?你可以在这里测试它 http://monopolo11.ninja/tests/daycalc2.php - monopolo11
@MarcinOrlowski,由于我刚开始学习,可能会有很多错误,所以非常感谢您的建议:D - monopolo11

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接