简体   繁体   中英

How to display table data by today's date?

I have a database called gameschedules and a table called dec1212. Inside of this table are the columns date (YYYY-MM-DD) , division , field , time , team1 , and team2 in that order.

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. 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:

$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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM