user1881090
user1881090

Reputation: 741

How to disable option if current time or date is not met

My question is that how can I disable options in the create drop down menu below if the start date $dbSessionDate and Start Time $dbSessionTime are before the current date and time?

Below is the drop down menu where data is fetched from database using mysqli:

while ( $sessionqrystmt->fetch() ) {

$time_string = $dbSessionDate." ".$dbSessionTime;
$time_from_string = strtotime($time_string);    
if($time_from_string > date("Y-m-d H:i:s"))
{
$sessionHTML .= sprintf("<option value='%s'>%s - %s - %s</option>", $dbSessionId, $dbSessionName, date("d-m-Y",strtotime($dbSessionDate)), date("H:i",strtotime($dbSessionTime))) . PHP_EOL;    
}else{
$sessionHTML .= sprintf("<option value='%s' disabled>%s - %s - %s</option>", $dbSessionId, $dbSessionName, date("d-m-Y",strtotime($dbSessionDate)), date("H:i",strtotime($dbSessionTime))) . PHP_EOL;   

}
}

UPDATE:

HTML from view Source:

<select name="session" id="sessionsDrop">
<option value="">Please Select</option>
<option value='28'>LDREW - 09-01-2013 - 09:00</option>
<option value='29'>BQNYF - 10-01-2013 - 10:00</option>
<option value='22'>WDFRK - 17-01-2013 - 09:00</option>
<option value='26'>POKUB1 - 25-01-2013 - 15:00</option>
</select>

Upvotes: 3

Views: 484

Answers (2)

0xmtn
0xmtn

Reputation: 2670

Use strtotime(). Combine $dbSessionDate and $dbSessionTime together, pass it to the function strtotime, then compare the resultant variable to date().

Code:

$time_string = $dbSessionDate." ".$dbSessionTime;
$time_from_string = strtotime($time_string);

if($time_from_string > strtotime(date("Y-m-d H:i:s")))
{
   //Do whatever you like to do.
}

Upvotes: 1

Juan de Parras
Juan de Parras

Reputation: 778

You need compare date and time and when its before the current date and time you need to put "disabled" attribute to dropdown.

Something like this:

while ( $sessionqrystmt->fetch() ) {
    $attribute = "";
    if (strtotime($dbSessionDate." ".$dbSessionTime) < date("Y-m-d H:i:s")){ $atribute = "disabled";}
    $sessionHTML .= sprintf("<option value='%s' %s>%s - %s - %s</option>", $dbSessionId, $attribute, $dbSessionName, date("d-m-Y",strtotime($dbSessionDate)), date("H:i",strtotime($dbSessionTime))) . PHP_EOL;
}

Here you can see disabled attribute definition: http://www.w3schools.com/tags/att_option_disabled.asp

Upvotes: 0

Related Questions