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( strtotime($database_date) > strtotime('now') ) {
...
I have used this one and it served the purpose:
if($date < date("Y-m-d") ) {
echo "Date is in the past";}
BR
if(strtotime($db_date) > time()) {
echo $db_date;
} else {
echo 'go ahead';
}
Use strtotime to convert any date to unix timestamp and compare.
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.
Here's a list of all possible checks for …
"Did a date pass?"
$date = strtotime( $date );
$date > date( "U" )
$date > mktime( 0, 0, 0 )
$date > strtotime( 'now' )
$date > time()
$date > abs( intval( $_SERVER['REQUEST_TIME'] ) )
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.
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' )
);
time()
still seems to be the fastest.