0

Possible Duplicate:
PHP - How can I check if the current date/time is past a set date/time?

I have a drop down menu below where it shows an exam's name, date and time.

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

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


$sessionHTML .= '</select>';

What I want to do is that if an exam's date and time is passed the current date and time, then display the text of those options in red, if they have not been passed make those options green color text.

E.g Current Date/Time: 24/09/2012 09:00:00 (this is just an example, I want the actual current date and time.

AAA - 13/08/2012 - This text should be red
BVF - 27/01/2013 - This text should be green

Does anyone know how to do this?

Community
  • 1
  • 1
CMB
  • 37
  • 2
  • 7

3 Answers3

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

while ( $sessionqrystmt->fetch() ) {
    if(strtotime(date("d-m-Y h:i:s")) > 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;  
}

Compare your current date with $dbSessionDate then determine the class.

$now = date("Y-m-d h:i:s");
$date = "2012-11-08 07:00:35";

echo $now."<br/>".$date."<br/>";

if(strtotime($now) > strtotime($date)){
    echo "RED";
} else {
    echo "GREEN";
}

Here's another simple example about comparing the time. You can adjust your code according to this I guess.

Joseph
  • 238
  • 1
  • 9
  • Im guessing your code looks for current time as well as current date? – CMB Nov 08 '12 at 03:41
  • Hi, only problem I realise is that it is showing for example an exam where the current date is today but the time is lets say 10:00:00, it is displaying it as red when it really should be green as that time in the current date is still upcoming. Can the hour be changed to 24 hours in h:i:s? – CMB Nov 08 '12 at 04:00
  • can you tell me the date format of "$dbSessionDate" from your db? – Joseph Nov 08 '12 at 04:05
  • The dateformat in db is year-month-day so it goes like this for example: 2012-02-23 – CMB Nov 08 '12 at 04:53
0
if ( time( ) > strtotime( "date here" ) ) {
    // current time is greater than date
}

:)

Xavian Tracy
  • 127
  • 1
  • 10
  • Im guessing this looks for both current date and time, even tohugh you are ust comparing with time()? – CMB Nov 08 '12 at 03:42
0

Do you want to do this in PHP or javascript? It seems to be better to use PHP since you have no idea whether the client's system clock is set to the correct time.

There is an assumption that the client is in the same timezone as the server, so people visiting from other locations (students on holiday or study trip perhaps) may get incorrect information.

In any case, the time that the view was last updated should also be shown.

RobG
  • 142,382
  • 31
  • 172
  • 209
  • Yeah php sounds better to do it in because it should be same time as the server which is more likely the correct time – CMB Nov 08 '12 at 03:53