0

I'm using PHP to insert events from a SQL database into a table in HTML, and I'm trying to make it only display future events (or events taking place the same day). The events are stored in the SQL database with "start" column having dates like 2014-12-25 00:00:00.

I was previously using this code to display all the dates:

while($row = mysql_fetch_array($result)){
    echo "<tr><td>" . $row['title'] . "</td><td>" . date("l, F d Y", strtotime($row['start'])) . " </td></tr>";
  }

And it worked fine. When I tried to compare the dates to yesterday (to ensure that events today would display) the HTML page was completely blank. Here's the code I'm using:

while($row = mysql_fetch_array($result)){
  if(strtotime("yesterday") < strtotime($row['start'])) {
    echo "<tr><td>" . $row['title'] . "</td><td>" . date("l, F d Y", strtotime($row['start'])) . "</td></tr>";
  }
}

I don't see anything wrong with this, but for some reason it works. I did some debugging and

strtotime($row['start']) outputs 1419483600

strtotime ("yesterday") outputs 1419742800

What am I doing wrong?

Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
xSpartanCx
  • 281
  • 3
  • 10
  • 25
  • 1
    You said that one output is 1419483600 and the other is 1419742800, is there actually a record that has `strtotime($row['start'])` value higher than 1419742800? ;) – Sebi Dec 30 '14 at 00:26
  • 3
    You seem to be missing a closing `)` in your `if` statement. – showdev Dec 30 '14 at 00:27
  • showdev is right, have you got startup error reporting enabled on your server? If not, syntax errors will "blank" the page. – Sebi Dec 30 '14 at 00:29
  • The closing `)` was due to copy/pasting error. – xSpartanCx Dec 30 '14 at 00:56
  • 1
    As a debugging exercise, what happens if you remove the if statement? Specifically what dates show up? In addition paste the `strtotime()` value. Also, what timezone are your dates stored in the database? The unix time is UTC/GMT I think? – Gohn67 Dec 30 '14 at 00:58
  • 2
    You should filter your data in your queries, look at this question to filter your data instead of filtering after all the data is pulled. It will simplify your code, increase efficiency and lower execution time. http://stackoverflow.com/questions/5182275/datetime-equal-or-greater-than-today-in-mysql – Ozair Patel Dec 30 '14 at 01:02
  • Huh, maybe I was missing something. I just retyped the entire while loops and now it seems to work. Thanks everyone for their help, though! – xSpartanCx Dec 30 '14 at 01:06
  • better way to filter your data by date in your SQL query, just use something like `select * from events where startdate > sysdate` – BSeitkazin Dec 30 '14 at 03:35

0 Answers0