wilna Posted May 19, 2011 Share Posted May 19, 2011 Hi, my query doesn't work, I've got a date field in my MySql table, I want to get results of all employees that was added in a certain period. My query doesn't work but as soon as I type in values in my query instead of variables it works, what am I doing wrong? $date = date('Y/m'); $date1 = strtotime('-6 month'); $date2 = strtotime('-6 month'); echo date('Y', $date1); echo date('m', $date2); $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbname", $con); $sql = "SELECT * FROM detail WHERE year(engaged) > '$date1' and month(engaged) > '$date2'"; $result=mysql_query($sql); echo mysql_num_rows($result); Quote Link to comment https://forums.phpfreaks.com/topic/236863-php-query-with-variables-dates/ Share on other sites More sharing options...
mikosiko Posted May 19, 2011 Share Posted May 19, 2011 echo the values of $date1 and $date2 (do not format them) and check what are you getting... after that read the strtotime() definition http://php.net/manual/en/function.strtotime.php Quote Link to comment https://forums.phpfreaks.com/topic/236863-php-query-with-variables-dates/#findComment-1217568 Share on other sites More sharing options...
biggieuk Posted May 19, 2011 Share Posted May 19, 2011 $date1 = strtotime('-6 month'); $date2 = strtotime('-6 month'); Should be $date1 = date ("Y/m", strtotime("-6 month", strtotime($date))); Quote Link to comment https://forums.phpfreaks.com/topic/236863-php-query-with-variables-dates/#findComment-1217570 Share on other sites More sharing options...
wilna Posted May 19, 2011 Author Share Posted May 19, 2011 thanks all I'll try it tonight! Quote Link to comment https://forums.phpfreaks.com/topic/236863-php-query-with-variables-dates/#findComment-1217600 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.