简体   繁体   English

如何按今天的日期显示表格数据?

[英]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) divisionfieldtimeteam1team2的顺序。

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.

相关问题 如何从** created_at ** date = ** today's date **的表中查询数据? - How to query data from a table where **created_at** date = **today's date** ? 如何仅在 codeigniter 中显示从今天的日期和月份到未来 3 个月的数据? - How to display data from today's date and month to the next 3 months only in codeigniter? 如何从今天的日期开始显示月份 - laravel - How to display the month ahead from today's date - laravel 我如何获取今天和之前的数据? - How can i get data from today's date and before? 从mysql表中获取数据,其中一列包含字符串格式的日期并与今天的日期匹配 - Fetch data from mysql table where a column contains date in string format and matches with today's date 如何使用PHP检查日期是否是今天的日期 - How to check if date is today's date with PHP 如何使Wordpress帖子输出按帖子标题中指定的日期排序? 以及如何显示标题为> =今天的日期的帖子? - How to make Wordpress posts output sorted by the date specified in the post title? And how to display posts with a date in the title > = today's date? 如何添加[今天的日期+1天] - How to add [today's date +1 day] 如何从今天起减去4个月? - How to subtract 4 months from today's date? 如何在mysql查询中插入今天的日期? - How to insert today's date in mysql query?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM