I have a database table something like this:
Col_1 | Datetime
test1 2015-12-19 09:00:00
test2 2015-12-18 12:30:00
test3 2015-12-19 10:00:00
test4 2015-12-19 16:45:00
I am trying to select in my query all results where the datetime is equal to today's date, although when I simply use something like:
$today = date("Y-m-d");
SELECT * FROM table WHERE datetime = '$today'
It just doesn't seem to work? And if I add the time in to $today it would only select those results which are equal to the exact current time which is not what I want?
Any idea as to how I can do this? I've even gone so far as to this which is totally stupid (Although it was about 3am in the morning when I coded it):
$now = date("Y-m-d H:m:s", strtotime('midnight'));
$todays = date(strtotime('midnight'));
$take24hours = date("Y-m-d H:m:s", strtotime('-24 hours', $todays));
$results = mysqli_fetch_assoc(mysqli_query($conn, "SELECT * FROM table WHERE date_added > '$take24hours' AND date_added < '$now')"));
解决方案
Can do this entirely in sql transaction ( no need for php date formatting ) :
SELECT * FROM table WHERE date(`datetime`) = current_date;