Welcome Guest, Not a member yet? Register   Sign In
Strtotime within a Model?
#1

[eluser]Stenson[/eluser]
Is it possible to have the strtotime within a Model?

What I want to do is select all the timestamps within the database which are below the value of 1209600 (2 Weeks in integer). I just want to find the number of rows in the table that have been added within the last two weeks.

for example a function similar to this
Code:
function myFunction() {
$currentTime = time();
$time = strtotime($currentTime);
$twoweeks = $time - 1209600;

$this->db->where(strtotime('myDate <'), $time);
$query = $this->db->get('tablename');

if($query->num_rows > 0 ) {
  $numOfRows = $query->num_rows();
  return $numOfRows;
}

}

Even a rough idea on how to do it within the traditional SELECT * FROM way would help me.

I hope someone can guide me in the right direction.

Thanks
CI Community
#2

[eluser]xeroblast[/eluser]
if you want to know the exact number of items you inserted in the last 2 weeks or any number of days then you can do it this way...

Code:
$this->db->where('myDate >=','DATE_ADD(NOW(), INTERVAL -14 DAY)',FALSE);
$query = $this->db->get('tablename');
return $query->num_rows();

it returns the number of rows in the result including 0...
#3

[eluser]Aken[/eluser]
Hopefully your DB supports that SQL syntax. If you have MySQL you should be golden.

Otherwise there's nothing wrong with using strtotime() inside your model.
#4

[eluser]sophistry[/eluser]
as long as your myDate field (as shown in your example code) is a timestamp field you don't need any strtotime() functions. in fact, the first one is just unnecessary and the second is just wrong: you can't evaluate a field name in strtotime().

start with something simple and try to take it step by step. maybe the strtotime() manual entry would help?

cheers.
#5

[eluser]Stenson[/eluser]
Thanks for the help Chaps!

Much appreciated!




Theme © iAndrew 2016 - Forum software by © MyBB