I want to load date and time in dropdown box.If current day is selected, time should start 2 hours from now. So far, I have loaded the date and time. One problem is that, when I select the next day, it not showing from start time. Also when I save the field to mysql database, it just saves first time alone. Suppose if the time selected is 18:00-19:00, It only saves 18. I have used varchar field.
So far my work are
<script type="text/javascript">
$( document ).ready(function() {
var d = new Date();
var hour = d.getHours()+2;
$('#timeInterval option[value="'+hour+'"]').prop('selected', true);
});
$(".form-control[name=dtime]").change(function(){
selDate = $(this).children(":selected").val();
var d = new Date();
var month = d.getMonth()+1;
var day = d.getDate();
var hour = d.getHours()+2;
var curDate = (day<10 ? '0' : '') + day + '-' +
(month<10 ? '0' : '') + month + '-' +
d.getFullYear();
// If selected date is equal to the current Date
if(selDate != curDate){
$('#timeInterval option:eq(0)').prop('selected', true); // Select 9:00 AM
}else {
$('#timeInterval option[value="'+hour+'"]').prop('selected', true); // Select the next 2 Hours
});
</script>
<select class="form-control" name="dtime" onchange="javascript:valueselect(this)" >
<option value="<?php echo date('d-m-Y') ;?>"><?php echo date("d-m-Y", time()) ;?></option>
<option value="<?php echo date("d-m-Y", time()+86400) ;?>"><?php echo date("d-m-Y", time()+86400) ;?></option>
<option value="<?php echo date("d-m-Y", time()+172800) ;?>"><?php echo date("d-m-Y", time()+172800) ;?></option>
</select>
<?php
echo "<select name='dnotes' id='timeInterval'>";
$now = new DateTime();
$s1=$now->format('H')+2;
$starttime='09:00';
$endtime='23:00';
$start = new DateTime($starttime);
$end = new DateTime($endtime);
$interval = new DateInterval('PT1H'); // Set the interval to One hour
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $dt)
{
$dt2 = clone $dt;
$dt2->add(new DateInterval('PT1H')); // Add One hour
echo '<option type="time" value="'.$dt->format("G").'">'.$dt->format('H:i').' - '.$dt2->format('H:i').' </option><br />';
}
echo "</select>";
?>