Issue
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 current date, so when i select that option lets say in 02/08/2015 (dd-mm-yy) and the second date to 04/08/2015 it returns one day but if i pick the first date manually (same 02/08/2015) it returns two days.
Examples: http://monopolo11.ninja/tests/imgs/ (the photos are there since i could put it in the post because of my low reputation)
Why is this happening?
Solution
You always override the $date1
var.
if ($tipfech == "actual"){
$yr1 = date("Y");
$month = date("m");
$day1 = date("d");
$date1 = new DateTime("$yr1-$month-$day1");
}
$date1 = new DateTime("$da1");
When you don't fill manually the start date, then new DateTime
is created with argument NULL
. It returns the current date but with the time actual.
This cause a compute day rounded down (because less of 48 hours).
Answered By - Luca Rainone
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.