Hello everyone: I am trying to get a data pull from MySQL within a PHP file that I have. The user passes in a date (represented currently by ‘$selectedDate’), and I want the query to pull all events that match that date.
The problem is that the MySQL date is in a unix format, meaning it has hours, seconds, etc. The passed in date is date only, so there won’t be an exact match.
So what I am trying to do is take my date, add a variable that is minus 1 day and another one that is plus one day and pull the events in based on that range.
The problem is that it’s not working and I have no idea why! After about 2 days of trouble shooting and searching, I am relying on you my stackoverflow friends!
Any ideas?
$selectedDate = '9-20-2012';
$dateComponents = preg_split('~[+*/-]~', $selectedDate);
$date1 = mktime(0, 0, 0, ( (int)$dateComponents[0] ), (int)$dateComponents[1]-1, (int)$dateComponents[2]);
$date2 = mktime(0, 0, 0, ( (int)$dateComponents[0] ), (int)$dateComponents[1]+1, (int)$dateComponents[2]);
$query = "SELECT id,title,start, DATE_FORMAT(start, '%Y-%m-%d %H:%i' ) AS startDate, end, url, className FROM $tableName WHERE start BETWEEN '$date1' AND '$date2' ORDER BY start";
$result = mysql_query($query) or die(mysql_error());
$array = array();
while($row = mysql_fetch_assoc($result)){
echo($row);
$array[] = $row;
}
Thanks!
You can add the Minutes and Seconds to your dates before passing them into SQL.