Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Letting MySQL figure out date intervals vs. PHP

In a MySQL 5.1 InnoDB environment, what is the best way to SELECT data based on date intervals?

Letting MySQL do it via something like DATE_SUB(CURDATE(), INTERVAL 5 DAY) > created_at

Or have PHP prepare the date before submission via strtotime?

like image 605
djdy Avatar asked Oct 21 '25 03:10

djdy


2 Answers

I would do it via the query to MySql. That way, you keep the logic of selecting dates out of the PHP. The PHP just handles the display, and you get the advantage of smaller chunks of data coming out of the database as well.

like image 147
Alan Delimon Avatar answered Oct 23 '25 18:10

Alan Delimon


I don't think it matters from performance point of view in this case. Your expression (DATE_SUB) will be evaluated just once. Another point is that your webserver and mysql server can use different timezones, so you may have different results.

like image 43
a1ex07 Avatar answered Oct 23 '25 17:10

a1ex07



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!