Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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

What I want to do is check the current date;

  • if the current date is before the date coming from the database then it needs to print the database date
  • if its after then it needs to print 'go ahead'. Could anyone point me in the right direction?
like image 731
Chris Avatar asked Nov 10 '10 22:11

Chris


People also ask

How do you check if a date is current date in PHP?

php function findDayDiff($date){ $param_date=date('d-m-Y',strtotime($date); $response = $param_date; if($param_date==date('d-m-Y',strtotime("now"))){ $response = 'Today'; }else if($param_date==date('d-m-Y',strtotime("-1 days"))){ $response = 'Yesterday'; } return $response; } ?>

How can I check if a date is greater than today in PHP?

php $date_now = time(); //current timestamp $date_convert = strtotime('2022-08-01'); if ($date_now > $date_convert) { echo 'greater than'; } else { echo 'Less than'; } ?> Show activity on this post.

What is Strtotime PHP?

The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.

How do you check if data is present in database?

To check whether a particular value exists in the database, you simply have to run just a regular SELECT query, fetch a row and see whether anything has been fetched. Here we are selecting a row matching our criteria, then fetching it and then checking whether anything has been selected or not.


2 Answers

if( strtotime($database_date) > strtotime('now') ) { ... 
like image 63
Cfreak Avatar answered Sep 27 '22 22:09

Cfreak


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.

like image 20
kaiser Avatar answered Sep 27 '22 21:09

kaiser