简体   繁体   English

PHP mysql选择日期范围

[英]PHP mysql select date range

I have a invoice type form submission and every submission has a date so I have created a varcha date field in mysql database to save the time.Time format is like '2011-06-26'.Now I need to select a range based on the date from my database.I tried following options but they don't display any result just blank page is displayed,even no errors. 我有一个发票类型表单提交,每个提交都有一个日期,所以我在mysql数据库中创建了一个varcha日期字段来节省时间。时间格式就像'2011-06-26'。现在我需要选择一个范围基于我的数据库中的日期。我尝试了以下选项,但它们不显示任何结果只显示空白页,甚至没有错误。

Should I follow some special techniques on saving date to my database.If so please give me some explain about that because I am new to PHP and MYSQL development. 我应该遵循一些特殊的技术来保存日期到我的数据库。如果是这样,请给我一些解释,因为我是PHP和MYSQL开发的新手。

SELECT * FROM table_name 
WHERE 'date' 
BETWEEN UNIX_TIMESTAMP('2011-06-02') AND UNIX_TIMESTAMP('2011-06-25')




SELECT * FROM my_table 
WHERE 'date' 
BETWEEN CAST('2011-06-02' AS DATE) AND CAST('2011-06-25' AS DATE)";

This is what I used to extract data 这是我用来提取数据的

$result=mysql_query($query);
  while($row=mysql_fetch_array($result)){
      echo $row['tax']."<br>";
  }

Thank you. 谢谢。

If you use DATETIME type to store your date the trick is simple. 如果您使用DATETIME类型来存储日期,那么技巧很简单。 DATETIME allows you to store the date and the timestamp, however if only the date is specifed, timestamp will be stored as 00:00:00 DATETIME允许您存储日期和时间戳,但是如果仅指定日期,则时间戳将存储为00:00:00

I'll show you a simple example. 我将向您展示一个简单的例子。

mysql> CREATE TABLE test_table(
       id INT UNSIGNED AUTO_INCREMENT,
       test_date DATETIME,
       PRIMARY KEY (id));

mysql> INSERT INTO test_table(test_date) 
       VALUES('2011-06-26'), ('2011-05-14'), ('2011-05-02');

mysql> SELECT * FROM test_table;
+----+---------------------+
| id | test_date           |
+----+---------------------+
|  1 | 2011-06-26 00:00:00 |
+----+---------------------+
|  2 | 2011-05-14 00:00:00 |
+----+---------------------+
|  3 | 2011-05-02 00:00:00 |
+----+---------------------+

mysql> SELECT * FROM test_table WHERE test_date
       BETWEEN '2011-05-01' AND '2011-05-31';
+----+---------------------+
| id | test_date           |
+----+---------------------+
|  2 | 2011-05-14 00:00:00 |
+----+---------------------+
|  3 | 2011-05-02 00:00:00 |
+----+---------------------+

That's it. 而已。

  1. Name of column - or without or in backticks 列的名称 - 或没有或在反引号中
  2. If date is varchar - also need cast date SELECT * FROM my_table WHERE cast( date as DATE) BETWEEN CAST('2011-06-02' AS DATE) AND CAST('2011-06-25' AS DATE)"; 如果date是varchar - 还需要转换日期SELECT * FROM my_table WHERE cast( date as DATE)BETWEEN CAST('2011-06-02'AS DATE)AND CAST('2011-06-25'AS DATE)“;

If the date column is of the TIMESTAMP or DATETIME type then the query should be: 如果date列是TIMESTAMP或DATETIME类型,那么查询应该是:

SELECT * FROM table_name 
WHERE 'date' 
BETWEEN '2011-06-02' AND '2011-06-25'

Edit: Sorry didn't see the column was a VARCHAR - you should change it to the DATETIME type to make things simpler. 编辑:抱歉没有看到列是VARCHAR - 您应该将其更改为DATETIME类型以使事情更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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