CodeIgniter Forums
DateTime, TimeStamp, and Time Comparisons in MySQL - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: DateTime, TimeStamp, and Time Comparisons in MySQL (/showthread.php?tid=30250)



DateTime, TimeStamp, and Time Comparisons in MySQL - El Forum - 05-07-2010

[eluser]Vik[/eluser]
Here's an addition to the other excellent threads on this forum regarding this topic.

After having spent a lot of the last day trying to figure out whether to use DateTime or TimeStamp fields, and how to do time comparisons with them using CI, here's an approach I found that seems to work.

First, which field type to use: it appears you can use either one, in the sense that MySQL time functions seem to accept them interchangeably. I prefer DateTime because it's more human-readable if you browse your database using software like phpMyAdmin, Navicat, etc.

Second, here's a CI-oriented approach to doing time comparisons that works:
Code:
$this->db->where("myTable.date_added > DATE_SUB(CURDATE(), INTERVAL 7 DAY)");
$query = $this->db->get('myTable');

This selects all records from MyTable where date_added is more recent than 7 days.