How do you check if a date is before another date in php?

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

  • Read
  • Discuss
  • 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.


    How do you check if a date is greater than another date in PHP?

    php $date1 = "2010-01-15"; $date2 = "2020-12-14"; $timestamp1 = strtotime[$date1]; $timestamp2 = strtotime[$date2]; if [$timestamp1 > $timestamp2] echo "$date1 is greater than $date2"; else echo "$date1 is less than $date2"; ?>

    How can I compare two dates in PHP?

    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. echo "$date1 is older than $date2" ; ?>

    How can I compare today's date with another date in PHP?

    Once you have created your DateTime objects, you can also call the diff[] method on one object and pass it the other date in order to calculate the difference between the dates. This will give you back a DateInterval object. $last = new DateTime[ "25 Dec 2020" ]; $now = new DateTime[ "now" ];

    What does date [] do in PHP?

    The date/time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date/time functions to format the date and time in several ways.

    Chủ Đề