PHP - sql query with condition on date

I have a little simple problem. I want to perform a query on a Mysql table to select posts posted after a certain date but I don't manage to get it working by setting that date as a PHP variable.

The following query doesn't return anything:

$reqsql = "SELECT * FROM posts WHERE post_date <= " . $from_date;

The "post_date" field is set as a datetime. I have tried everything for the $from_date PHP variable format:

$from_date = date();
$from_date = date('Y-m-d-H-i-s');
$from_date = date('Y-m-d H:i:s');
$from_date = strtodate("now");

None of these work...

But it works fine if I put a basic Mysql date like:

$reqsql = "SELECT * FROM posts WHERE post_date <= NOW()";

Any idea? Thanks!

This question and answers originated from www.stackoverflow.com
Question by (8/24/2011 2:47:57 PM)

Answer

You probably only need to wrap $from_date in single quotes.

$reqsql = "SELECT * FROM posts WHERE post_date <= '" . $from_date ."'";
Answer by

Find More Answers
Related Topics  php  mysql  query  date
Related Questions
  • mysql date storage & query performance with php

    I've got a database of historical records from WW2 and currently each recorded event's date is stored in one integer field as YYMMDDHHMM. This makes simple tasks like ORDER BY, or searching for all …
  • sql query date time

    i have a datestamp field in my table structure. using sql, i want to find any user who registered in yesterdays date using these time range. eg: 2010-02-06 14:00:00 2010-02-07 10:00:00 i will…
  • SQL 3 column query on condition

    I'm still pretty new to SQL and have a table something like the following Street | Family | Car -------------------------------- Mayfair | Jones | BMW Mayfair | Jones | Ford Mayfair | Jone…
  • SQL query on a condition

    I'm writing a query to retrieve translated content. I want it so that if there isn't a translation for the given language id, it automatically returns the translation for the default language, with …
  • Build condition with date for jpa2 typesafe query

    I have following query: SELECT DISTINCT * FROM Projekt p WHERE p.bewilligungsdatum = to_date('01-07-2000', 'dd-mm-yyyy') but i have problems to build the conditions. Here my code: conditi…
  • PHP MySQL Date Comparsion

    I Have a Mysql with the rows Data1,data2,data3...Data7 and i want to compare with the current date,searching the internet i got this so far: $curDate = date("Y-m-d"); $query = "SELECT Id FROM Pro…
  • How to insert date(y-m-d) into MySQL database?

    I have a field of type date in MySQL. When I try to insert any date in this field with PHP using following query, It stores 0000-00-00 in that field. For example: UPDATE test SET dob=2000-09-2…
  • sqlite select with condition on date

    I have an sqlite table with Date of Birth.I would like to write a query to select those records where the age is more then 30 . I have tried following but it doesnot work. select * from mytable w…
  • PHP + MySQL query : Unexpected results (for small number of results only)

    Trying to show results from the MySQL DB based on the start date and end date given by the user. Following is the code : //Getting Values From Other Page $start_date = date("Y-m-d", strtotime($…
  • query sql convert text field dd/mm/yyyy to date field yyyy-mm-dd

    i've imported a CSV file into mysql with dates dd/mm/yyyy Now need a query to convert it from text to date format yyy-mm-dd Cheers S.