PHP Condition If Date is Past else If Current of Future Condition

Hi all,

I am using some code to check if a date is passed or not.

The format of the date is: 2013-08-22

The code below works fine for past dates but It's defining current dates as past.

For example if $eventdate was 2013-09-26 which is today's date the output would be: past date.

Here is the current code:


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

	if($eventdate < $datenow) {
		

		echo 'past date';
	
	} else {


		echo 'Not past date';


	}

Open in new window



      What I need the code to do is:

      
if (EventDate is Past Date) {

		echo 'The is a past date';
	
	} else // IF Date is TODAY or FUTURE  {


		echo 'Current of Future date';

	}

Open in new window


      How can I do this please?
LVL 1
error2013Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MiramacesCommented:
Use the "format()" function of the DateTime object so you only compare your dates on a daily base:
$eventdate = new DateTime($event['date']);
$datenow = new DateTime();

if($eventdate->format('Y-m-d') < $datenow->format('Y-m-d')) {
	echo 'past date';
} else {
	echo 'Not past date';
}

Open in new window

otherwise you did something like this:
if("2013-09-26 00:00:00" < "2013-09-26 07:08:09")

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
InsoftserviceCommented:
$event ="2013-09-25";
         $today = date("Y-m-d");
      
        $todaydate = strtotime($today);
        $eventdate = strtotime($event);

        if($todaydate <=$eventdate)
         {
            echo "todays date is less than event date";
         }
        else
        {
            echo "todays date is greater than event date";
         }
0
Ray PaseurCommented:
This article will cover the question as well as many other things you need to know about DATETIME processing in PHP and MySQL.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

In the instant case, the code is comparing objects to see if one is less than the other.  It should be comparing the properties of the objects, not the entire objects.  You can use PHP var_dump() to print out the properties of the object, so you can see which ones to compare.  You will find that you want to compare DateTime::date properties.
0
InsoftserviceCommented:
@error2013 Did you tried my comments
0
error2013Author Commented:
I didn't get to as I tried Miramaces's one which was posted first and it's worked fine so I used that one.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.