Domanda

I have a drop down menu which displays an exam's name, date and time and what I want to do is that if an exam date has been passed from the current date or time, then the text is displayed in red, else text is green.

Problem I am having is that if the current date is 10-11-2012 (which is today) and if an exam has this date and time below:

10-11-2012, 16:00:00

It displays the text in red and not in green. The problem is that it is not recognising the current time, it thinks that as it is the current date, it should be red, but because the current time hasn't passed in the current date, then it should be green.

The database stores the date as 2012-11-10 and the time in the database stored as 16:00:00. While in the drop down as mentioned it is stored as: 10-11-2012, 16:00:00

Below is the code:

<style>
.red{ color:red; }
.green{ color:green; }
</style>

...

    $now = date("Y-m-d h:i:s");
    while ( $sessionqrystmt->fetch() ) {
       if(strtotime($now) > strtotime($dbSessionDate)){
             $class = 'red';
        } else {
             $class = 'green';
        }
        $sessionHTML .= sprintf("<option value='%s' class='%s'>%s - %s - %s</option>", $dbSessionId, $class, $dbSessionName, date("d-m-Y",strtotime($dbSessionDate)), $dbSessionTime) . PHP_EOL;
        }

$now should be the current date and time and $dbSessionDate should be 10-11-2012

UPDATE:

$sessionHTML = '<select name="session" id="sessionsDrop">'.PHP_EOL;
$sessionHTML .= '<option value="">Please Select</option>'.PHP_EOL;           

$now = date("Y-m-d H:i:s");
echo strtotime($now).'<br>';
echo strtotime('10-11-2012, 16:00:00');
while ( $sessionqrystmt->fetch() ) {
   if(strtotime($now) > strtotime($dbSessionDate)){
         $class = 'red';
    } else {
         $class = 'green';
    }
    $sessionHTML .= sprintf("<option value='%s' class='%s'>%s - %s - %s</option>", $dbSessionId, $class, $dbSessionName, date("d-m-Y",strtotime($dbSessionDate)), date("H:i",strtotime($dbSessionTime))) . PHP_EOL;
    }


$sessionHTML .= '</select>';

$assessmentform = "<form action='".htmlentities($_SERVER['PHP_SELF'])."' method='post'>
<p><strong>Assessments:</strong> {$sessionHTML} </p>   
</form>";
È stato utile?

Soluzione

The code seems to be OK. Try:

$now = date("Y-m-d h:i:s");
echo strtotime($now).'<br>';
echo strtotime('10-11-2012, 16:00:00');

and check the server time.

UPDATE:

Replace:

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

with:

$sessionHTML .= sprintf("<option value='%s' style='color: %s'>%s - %s - %s</option>", $dbSessionId, $class, $dbSessionName, date("d-m-Y",strtotime($dbSessionDate)), date("H:i",strtotime($dbSessionTime))) . PHP_EOL;

to prevent css mistake.

Or replace:

strtotime($now) > strtotime($dbSessionDate)

with:

strtotime($now) < strtotime($dbSessionDate)
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top