tags:

views:

145

answers:

3

I'm trying to write a script that will check if the current date/time is past the 05/15/2010 at 4PM

How can I use PHP's date() function to perform this check? I can't think of a neat way to do it >.<

+4  A: 

Check PHP's strtotime-function to convert your set date/time to a timestamp: http://php.net/manual/en/function.strtotime.php

If strtotime can't handle your date/time format correctly ("4:00PM" will probably work but not "at 4PM"), you'll need to use string-functions, e.g. substr to parse/correct your format and retrieve your timestamp through another function, e.g. mktime.

Then compare the resulting timestamp with the current date/time (if ($calulated_timestamp > time()) { /* date in the future */ }) to see whether the set date/time is in the past or the future.

I suggest to read the PHP-doc on date/time-functions and get back here with some of your source-code once you get stuck.

Select0r
+2  A: 
if ( time( ) > strtotime( "05/15/2010 4:00PM" ) ) {
    // current time is greater than 05/15/2010 4:00PM
}
Salman A
+2  A: 

There's also the DateTime class which implements a function for comparison operators.

// $now = new DateTime();
$dtA = new DateTime('05/14/2010 3:00PM');
$dtB = new DateTime('05/14/2010 4:00PM');

if ( $dtA > $dtB ) {
  echo 'dtA > dtB';
}
else {
  echo 'dtA <= dtB';
}
VolkerK