0

I have a simple MySql DB with some logdata. I want get a forecast for events that repeats +-30min on a Weekday. eg: I compare 3 Events that match the criteria and give a alert.

And now the Question: What is the best method to compare the time? I can put mktime or date() via PHP into the DB or I do it direct with mysql.

Epsil0neR
  • 1,676
  • 16
  • 24
sarahsdev
  • 177
  • 6
  • 22

1 Answers1

1

What could be done on database level most certainly should be done there, considering performance.

SELECT * FROM tbl WHERE mytime >= DATE_SUB(NOW(), INTERVAL 30 MINUTE)

This will search everything that is at most 30 minutes ago and newer. Feel free to modify this query to fit your needs.

David Müller
  • 5,291
  • 2
  • 29
  • 33
  • "What could be done on database level most certainly should be done there, considering performance." I VERY MUCH DISAGREE!!! It's much easier to scale an application if PHP is the bottleneck by simply adding more servers running PHP. It's much harder to scale a database - and it's often the database that becomes the bottleneck way before PHP's performance. So why on earth would you want to pass processing time to the DB that could be worked out by PHP? Except of course, when it means the DB returning much more data than needed in order for PHP to simply discard results. – HenchHacker Nov 11 '12 at 20:29
  • In general, databases are increadibly fast when you just let them do their job they are intended for. I often get a headache when I see how people force PHP to do jobs that fall into the field of database logic. Even if performance is not a key factor in the application, you should take care to not mix up PHP times and mysql times because of timezone differences that might matter in a later stage. – David Müller Nov 11 '12 at 20:36