MySQL查询选择所有与日期时间相等的日期等于今天 [英] MySQL query select all were date is equal to today on datetime

查看:2198
本文介绍了MySQL查询选择所有与日期时间相等的日期等于今天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似这样的数据库表:

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

我试图在查询中选择datetime等于今天的日期的所有结果,尽管当我只是使用类似以下内容的情况时:

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'

$today = date("Y-m-d"); SELECT * FROM table WHERE datetime = '$today'

似乎不起作用?而且,如果我将时间添加到$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?

关于如何执行此操作的任何想法?我什至认为这完全是愚蠢的(尽管我在编码时大约是凌晨3点):

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')"));

推荐答案

可以完全在sql事务中完成此操作(无需php日期格式):

Can do this entirely in sql transaction ( no need for php date formatting ) :

SELECT * FROM table WHERE date(`datetime`) = current_date;

这篇关于MySQL查询选择所有与日期时间相等的日期等于今天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆