0

I am attempting to find records that fall between 2 dates. My dates are set in 2 php variables, today's date and then 8 weeks before today. The code works when the 2 variables are within the same year, but when the date falls in the previous year, no results are found; i.e. $today = 28 Jan 2014 and $lastmonth = 3 Dec 2013. The variables seem to be calculating correctly when I look at the date results. What is causing the problem?

<?php
$today = date('Y-m-d');
$lastmonth = date('Y-m-d', strtotime('-8 week', strtotime($today)));

$sql = mysql_query("SELECT * FROM products WHERE dated_release BETWEEN '$lastmonth' AND '$today' ORDER BY code");

$existCount = mysql_num_rows($sql); // count the row nums
if ($existCount > 0) { // evaluate the count and build display
// more code

?>

The dated_release field in the table has been set up as a date field. This is my first website using php and mysql.

1 Answer 1

0

you can create a simple query like this

SELECT *
FROM products
WHERE dated_release BETWEEN NOW()
    AND DATE_ADD(NOW(), INTERVAL -8 WEEK);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.