Welcome Guest, Not a member yet? Register   Sign In
DateTime, TimeStamp, and Time Comparisons in MySQL
#1

[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.




Theme © iAndrew 2016 - Forum software by © MyBB