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?
Interval Between Different Dates In order to compare those two dates we use the method diff() of the first DateTime object with the second DateTime object as argument. The diff() method will return a new object of type DateInterval .
php $m = date("m"); // Month value $de = date("d"); // Today's date $y = date("Y"); // Year value echo "Yesterday's date was: " . date('d-m-Y', mktime(0,0,0,$m,($de-1),$y)); ?>
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.
You can use strtotime()
and time()
:
if (strtotime($event['date']) < time()) { // past date }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With