[英]Getting results only for today's date
I want to get some of the entries from my db which has dates of only today, here is my working code which gets all of the data; 我想从数据库中获取某些条目,该数据库的日期只有今天,这是我的工作代码,它获取所有数据;
$result = mysql_query("SELECT *FROM products") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// products node
$response["products"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$product = array();
$product["pid"] = $row["pid"];
$product["name"] = $row["name"];
$product["price"] = $row["price"];
$product["description"] = $row["description"];
$product["created_at"] = $row["created_at"];
$product["updated_at"] = $row["updated_at"];
// push single product into final response array
array_push($response["products"], $product);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
}
As well as allowing the user to pick a date and display only the data that was created at that date. 以及允许用户选择一个日期并仅显示在该日期创建的数据。
The field is; 领域是;
name created_at 名称created_at
type timestamp 键入时间戳
null No 否否
default CURRENT_TIMESTAMP 默认值CURRENT_TIMESTAMP
In the Mysql query you can use DATE() to strip the time of day off of your timestamp like so: 在Mysql查询中,您可以使用DATE()从时间戳中剥离一天中的时间,如下所示:
SELECT * FROM products WHERE DATE(created_at) = '<date>';
ie this could be implemented as 即这可以实现为
$result = mysql_query("SELECT * FROM products WHERE DATE(created_at) = '".date("Y\-m\-d")."'") or die(mysql_error());
or if you have $inputdate set to be the user specified date. 或如果您将$ inputdate设置为用户指定的日期。
$result = mysql_query("SELECT * FROM products WHERE DATE(created_at) = '".$inputdate->format("Y\-m\-d\")."'") or die(mysql_error());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.