PHP gives me different results with the same values ββonly in different ways
This is my code:
<!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>
My "fecha actual" is the current date, so when I select this option on, say 02/08/2015 (dd-mm-yy) and the second date is 04/08/2015, it returns one day, but if i select the first date manually (same 02/08/2015) it returns two days.
Examples: http://monopolo11.ninja/tests/imgs/ (there are photos, since I can put it in the mail because of my low reputation)
Why is this happening?
Thanks for the advanced one.
source to share
You are always overriding $date1
var.
if ($tipfech == "actual"){
$yr1 = date("Y");
$month = date("m");
$day1 = date("d");
$date1 = new DateTime("$yr1-$month-$day1");
}
$date1 = new DateTime("$da1");
If you don't manually fill in the start date then it new DateTime
is created with an argument NULL
. It returns the current date, but with the actual time.
This will round off the day of the calculation (because it is less than 48 hours).
source to share