php - Creating where function with dates in sql query (Laravel 4) -
i have php function retrieving list of records based on 'created_at' column.
public function browseresults($days = 90) { $search_period_in_seconds = 60*60*24*$days; $today = time(); $reference_date = $today - $search_period_in_seconds; $query ="select * brands"; $query .=" created_at > ".$reference_date; $results = db::select($query); // laravel syntax performing query.
this isnt returning required results. think issue because mysql database storing dates in following format: 2013-08-04 15:54:42
compared unix timestamp. have tried inserting form of strtotime()
function sql caused error. there anyway of avoiding having pull of records database , doing comparison in php? i.e. possible perform filter in sql query?
many thanks
you check out mysql function from_unixtime:
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_from-unixtime
or create correct date php:
$datetime = new datetime("-" . $days . " days"); // minus $days days $date_sql = $datetime->format("y-m-d h:i:s"); // format date string ex. 2013-08-07 00:00:00 $query ="select * brands"; $query .=" created_at > '$date_sql'"; // etc.
Comments
Post a Comment