Chris
Chris

Reputation: 1129

PHP Checking if the current date is before or after a set date

Im pulling a date from a database which is formatted like dd-mm-YYYY.

What I want to do is check the current date;

Upvotes: 54

Views: 133934

Answers (10)

quant
quant

Reputation: 512

I have used this one and it served the purpose:

if ($date < date("Y-m-d")) {
    echo "Date is in the past";
}

BR

Upvotes: 3

kaiser
kaiser

Reputation: 22333

Here's a list of all possible checks for …

"Did a date pass?"

Possible ways to obtain the value

$date = strtotime( $date );

$date > date( "U" )
$date > mktime( 0, 0, 0 )
$date > strtotime( 'now' )
$date > time()
$date > abs( intval( $_SERVER['REQUEST_TIME'] ) )

Performance Test Result (PHP 5.4.7)

I did some performance test on 1.000.000 iterations and calculated the average – Ordered fastest to slowest.

+---------------------+---------------+
|        method       |     time      |
+---------------------+---------------+
|        time()       | 0.0000006732  |
|       $_SERVER      | 0.0000009131  |
|      date("U")      | 0.0000028951  |
|     mktime(0,0,0)   | 0.000003906   |
|   strtotime("now")  | 0.0000045032  |
| new DateTime("now") | 0.0000053365  |
+---------------------+---------------+

ProTip: You can easily remember what's fastest by simply looking at the length of the function. The longer, the slower the function is.

Performance Test Setup

The following loop was run for each of the above mentioned possibilities. I converted the values to non-scientific notation for easier readability.

$loops = 1000000;
$start = microtime( true );
for ( $i = 0; $i < $loops; $i++ )
    date( "U" );
printf(
    '|    date("U")     | %s  |'."\n",
    rtrim( sprintf( '%.10F', ( microtime( true ) - $start ) / $loops ), '0' )
);

Conclusion

time() still seems to be the fastest.

Upvotes: 44

user527892
user527892

Reputation:

I wanted to set a specific date so have used this to do stuff before 2nd December 2013

if(mktime(0,0,0,12,2,2013) > strtotime('now')) {
    // do stuff
}

The 0,0,0 is midnight, the 12 is the month, the 2 is the day and the 2013 is the year.

Upvotes: 7

Cfreak
Cfreak

Reputation: 19309

if( strtotime($database_date) > strtotime('now') ) {
...

Upvotes: 128

KGC
KGC

Reputation: 141

If you are looking for a generic way of doing this via MySQL, you could simply use a SELECT statement, and add the WHERE clause to it.

This will grab all fields for all rows, where the date stored in field "date" is before "now".

SELECT * FROM table WHERE UNIX_TIMESTAMP(`date`) < UNIX_TIMESTAMP()

Personally, I found this method to be more gentle on newbies in MySQL, since it uses the standard SELECT statement with WHERE to fetch the results. Obviously, you could grab only the fields relevant if you wanted to, by listing them instead of using a wildcard (*).

Upvotes: 4

Drew
Drew

Reputation: 116

if(strtotime($db_date) > time()) {
  echo $db_date;
} else {
  echo 'go ahead';
}

Upvotes: 1

robjmills
robjmills

Reputation: 18588

a MySQL-only solution would be something like this:

SELECT IF (UNIX_TIMESTAMP(`field`) > UNIX_TIMESTAMP(), `field`,'GO AHEAD') as `yourdate`
FROM `table`

Upvotes: 0

superfro
superfro

Reputation: 3302

if(strtotime($row['database_date']) > strtotime('now')) echo $row['database_date'];
else echo date("d-m-Y");

No need to check the hours because if they are on the same day it will show the same date either way...

Upvotes: 1

zerodin
zerodin

Reputation: 877

Use strtotime to convert any date to unix timestamp and compare.

Upvotes: 0

AndreKR
AndreKR

Reputation: 33668

if (strtotime($date) > mktime(0,0,0)) should do the job.

Upvotes: 2

Related Questions