Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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?

like image 868
Satch3000 Avatar asked Sep 26 '13 14:09

Satch3000


People also ask

How can I compare two dates in PHP?

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 .

How can I get previous date from DateTime in PHP?

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)); ?>


2 Answers

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.

like image 89
Amal Murali Avatar answered Sep 18 '22 19:09

Amal Murali


You can use strtotime() and time():

if (strtotime($event['date']) < time()) {     // past date } 
like image 25
newfurniturey Avatar answered Sep 22 '22 19:09

newfurniturey