I need help badly form my task. When I had selected the date such as 2015-05-08 it stored as 1970-01-01. I had tried all the solutions that I found but it still can't work well.
Below are my codes. Hope you guys can help. Appreciate it.
PHP files
<div id = "wrapper">
<div id = "Page1">
<div class="title">
<h3>Check-in / Check-out Date</h3></div>
<div class = "container">
<div style="padding: 10px 50px 0 20px; ">
<table align="center" class="bookingForm" style="color: #000;">
<tr>
<td>Check-in date:</td>
<td><input id="cid" type="text" name="cid" onchange="checkNight()"></td>
</tr>
<tr>
<td>Check-out date:</td>
<td><input id="cod" type="text" name="cod" onchange="checkNight()"></td>
</tr>
<tr>
<td>No. of night:</td>
<td><input id="night" type="text" value="0" name="night"></td>
</tr>
<tr>
<td></td>
<td><input type="button" value="Next" name="nextButton" id="nextButton" class="nextButton" onclick="next()" >
<input type="hidden" id="noRoom" style="display:none;" value="1">
</td>
</tr>
Javascript Code
$(document).ready(function() {
$("#cid").datepicker({
dateFormat: "yy-mm-dd",
minDate: 0,
onSelect: function(date) {
var date2 = $('#cid').datepicker('getDate');
date2.setDate(date2.getDate() + 1);
//sets minDate to dt1 date + 1
$('#cod').datepicker('option', 'minDate', date2);
checkNight();
}
});
$('#cod').datepicker({
dateFormat: "yy-mm-dd",
onClose: function() {
var dt1 = $('#cid').datepicker('getDate');
var dt2 = $('#cod').datepicker('getDate');
//check to prevent a user from entering a date below date of dt1
if (dt2 <= dt1) {
var minDate = $('#cod').datepicker('option', 'minDate');
$('#cod').datepicker('setDate', minDate);
checkNight();
}
}
});
});
function checkNight() {
var dateGet = new Date;
var dateGet2 = new Date;
dateGet = $('#cid').datepicker("getDate");
dateGet2 = $('#cod').datepicker("getDate");
if ($('#cid').val() != "" && $('#cod').val() != "") {
var diffDay = Math.abs(dateGet2 - dateGet);
diffDay = diffDay / 86400000;
if(diffDay<0){
}else{
document.getElementById('night').value = diffDay;
}
}
}
function next(){
var cid = $("#cid").val();
var cod = $("#cod").val();
var night = $("#night").val();
//if cid and cod are empty
if(cid == "" || cod == ""){
alert('Please key in the required field');
}
//redirect to next page if fuifil requirement
else{
window.location="http://localhost/reserve/app/room.php";
}
}
Storing Date Code
<?php
$cid = $_POST['cid'];
$cidDate = date('Ymd', strtotime($cid));
$cod = $_POST['cod'];
$codDate = date('Ymd', strtotime($cod));
$night = $_POST['night'];
$sql = ("INSERT INTO `check`(`cid`, `cod`, `night`) VALUES ('$cidDate','$codDate','$night')");
?> ?>