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; ie $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.
you can create a simple query like this
SELECT *
FROM products
WHERE dated_release BETWEEN NOW()
AND DATE_ADD(NOW(), INTERVAL -8 WEEK);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.