Comparing dates - doesn't seem to work

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mattpointblank
Forum Contributor
Posts: 304
Joined: Tue Dec 23, 2008 6:29 am

Comparing dates - doesn't seem to work

Post by mattpointblank »

Hi all,

I'm looping through a result set of events. I want to restrict events that have already happened from appearing in the output. I can't do this in my SQL query because some of the events have StartDate and EndDate fields, others have just StartDates, so I can't select all the events that start in the past and end in the future.

Anyway, here's my code (simplified):

Code: Select all

 
$today = date("Y-m-d H:i:s");
$compStartDate = strtotime($StartDate);
$compToday = strtotime($today);
 
if($compStartDate >= $compToday) {
 
  // output event info here
 
}
 
$StartDate is a result from my database, and has the same date structure as $today.

The code doesn't work though - it outputs nothing. I'm confident it's not down to me echoing out the results elsewhere, so the logic of the loop is wrong. Any ideas?
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Comparing dates - doesn't seem to work

Post by VladSun »

mattpointblank wrote:Hi all,

I'm looping through a result set of events. I want to restrict events that have already happened from appearing in the output. I can't do this in my SQL query because some of the events have StartDate and EndDate fields, others have just StartDates, so I can't select all the events that start in the past and end in the future.

Anyway, here's my code (simplified):

Code: Select all

 
$today = date("Y-m-d H:i:s");
$compStartDate = strtotime($StartDate);
$compToday = strtotime($today);
 
if($compStartDate >= $compToday) {
 
  // output event info here
 
}
 
$StartDate is a result from my database, and has the same date structure as $today.

The code doesn't work though - it outputs nothing. I'm confident it's not down to me echoing out the results elsewhere, so the logic of the loop is wrong. Any ideas?
Yes - debug:

Code: Select all

 
$today = date("Y-m-d H:i:s");
$compStartDate = strtotime($StartDate);
$compToday = strtotime($today);
 
echo $today."<br />";
echo $StartDate."<br />";
echo $compStartDate."<br />";
echo $compToday."<br />"; 
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply