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.
View Discussion
Improve Article
Save Article
View Discussion
Improve Article
Save Article
Given two dates [date1 and date2] and the task is to compare the given dates. Comparing two dates in PHP is simple when both the dates are in the same format but the problem arises when both dates are in a different format.
Method 1: If the given dates are in the same format then use a simple comparison operator to compare the dates.
Example:
Output:
1998-11-24 is latest than 1997-03-26
Method 2: If both of the given dates are in different formats then use strtotime[] function to convert the given dates into the corresponding timestamp format and lastly compare these numerical timestamps to get the desired result.
Example:
Output:
12-03-26 is latest than 2011-10-24
Method 3: Using DateTime class to compare two dates.
Example:
Output:
2012-11-24 is latest than 2011-03-26
PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.