[英]How to display table data by today's date?
I have a database called gameschedules and a table called dec1212. 我有一个名为gamechedules的数据库和一个名为dec1212的表。 Inside of this table are the columns
date (YYYY-MM-DD)
, division
, field
, time
, team1
, and team2
in that order. 这里面表是列
date (YYYY-MM-DD)
division
, field
, time
, team1
和team2
的顺序。
I already have the data displaying on my page but I need to restrict it to only showing rows in my database that have a date that is equal to the current date. 我已经在页面上显示了数据,但我需要将其限制为仅显示数据库中日期等于当前日期的行。 So if there are 10 rows and only 5 of them have today's date in the date column, only those should show.
因此,如果有10行,而其中只有5行在date列中有今天的日期,则仅应显示这些行。 Here is my current query code and I know it is wrong but if someone can help me correct it that would be great:
这是我当前的查询代码,我知道这是错误的,但是如果有人可以帮助我更正它,那就太好了:
Current code: 当前代码:
//specifies that it is getting data from the table and limited num rows
$result = mysql_query("SELECT * FROM `dec1212` LIMIT 0, 10 ") or die(mysql_error());
This is what I tried to do to restrict data by date: 这是我尝试通过日期限制数据的方法:
//set variable to current date with same format as date column
$myDate = date('Y-m-d');
//pull only rows that match the current date
$result = mysql_query("SELECT * FROM 'dec1212' WHERE date = $myDate()") or die(mysql_error());
I know that my code is incorrect so this is why I am asking for help. 我知道我的代码不正确,所以这就是为什么我寻求帮助。
试试这个只是信号线查询
$result = mysql_query('SELECT * FROM dec1212 WHERE DATE(CONVERT_TZ(date,"+00:00","-8.00")) = DATE(CONVERT_TZ(UTC_TIMESTAMP(),"+00:00","-8.00"))') or die(mysql_error());**
$myDate = date('Y-m-d');
//pull only rows that match the current date
$result = mysql_query("SELECT * FROM `dec1212` WHERE date = '$myDate'") or die(mysql_error());
Try this 尝试这个
$result = mysql_query("SELECT * FROM `dec1212` WHERE `date` = CURDATE()") or die(mysql_error());
Try This 尝试这个
//set variable to current date with same format as date column
$myDate = date('Y-m-d');
//pull only rows that match the current date
$result = mysql_query("SELECT * FROM 'dec1212' WHERE date = $myDate") or die(mysql_error());
You are using variable not the function so just change like this 您正在使用变量而不是函数,所以只需像这样更改
$myDate = date('Y-m-d');
$result = mysql_query("SELECT * FROM 'dec1212' WHERE date = '$myDate'") or die(mysql_error());
Make sure your date field in database containing the only date, not time 确保数据库中的日期字段仅包含日期,而不包含时间
$result = mysql_query("SELECT * FROM 'dec1212'
WHERE `date`= '$myDate'") or die(mysql_error());
if your date field containing datetime datatype then use: 如果您的日期字段包含datetime数据类型,则使用:
$result = mysql_query("SELECT * FROM 'dec1212'
WHERE `date`= date('$myDate')") or die(mysql_error());
让数据库完成工作,
SELECT * FROM `dec1212` WHERE DATE(date) = DATE(NOW())
Note that $myDate
is a variable, not a function. 请注意,
$myDate
是变量,而不是函数。 Thus, your code would be : 因此,您的代码将是:
//set variable to current date with same format as date column
$myDate = date('Y-m-d');
//pull only rows that match the current date
$result = mysql_query("SELECT * FROM `dec1212` WHERE date = '$myDate'") or die(mysql_error());
Table name or field name should be wrapped with carat `, not single quote '
or you may omit it. 表名或字段名应以克拉号“而不是单引号
'
否则您可以省略。
If you want to use MySQL NOW()
, you need to consider time zone conversion as Raj Mohan mentioned. 如果要使用MySQL
NOW()
,则需要考虑Raj Mohan提到的时区转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.