Skip to content
Advertisement

PHP Check if date is past to a certain date given in a certain format

I have a php event’s calender which queries the database to get the dates.

I display the event date using:

$event['date']

and this display’s in this format:

2013-07-31 for example.

Now, what I need to do is to check if this date is a past date to the current date.

How can I do this?

Advertisement

Answer

You can compare the dates with PHP’s DateTime class:

$date = new DateTime($event['date']);
$now = new DateTime();

if($date < $now) {
    echo 'date is in the past';
}

Note: Using DateTime class is preferred over strtotime() since the latter will only work for dates before 2038. Read more about the Year_2038_problem.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement