0

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>";
?>

1 Answer 1

1

Your value field of option tag has $dt->format("G")

Shouldn't it be $dt->format('H:i').' - '.$dt2->format('H:i') ?

Sign up to request clarification or add additional context in comments.

4 Comments

echo '<option value="'.$dt->format('H:i').' - '.$dt2->format('H:i').'">'.$dt->format('H:i').' - '.$dt2->format('H:i').' </option>';
That worked.... It saved the values to db. One more question, when I select the next date, it is not showing from the start time
I'm sorry. I need to hide the previous time for today's date... Suppose, If it shows 19:00-20:00, then time starting from 9:00-10:00 to 18:00-19:00 should hide...
check datetime in for loop and add continue. if(date is current && if current time[hours] is less than current) then continue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.