简体   繁体   English

检查英国日期是否在SQL中有两个日期

[英]Check if UK date is bewteen two dates in SQL

I need to check that a UK date, stored as string in the format dd/mm/yyyy in a database table ( postmeta.event_date ), is between two dates using SLQ format BETWEEN 'yyyy-mm-dd' AND 'yyyy-mm-dd+28' . 我需要检查在数据库表( postmeta.event_date )中以dd / mm / yyyy格式存储为字符串的英国日期是否在两个日期之间使用SLQ格式BETWEEN 'yyyy-mm-dd' AND 'yyyy-mm-dd+28' I'm ok with working out the dates to check between, it's the date to check that I'm having issues with because it is in the wrong format for SQL. 我正好确定要检查的日期,这是检查我遇到问题的日期,因为它的SQL格式错误。

I have no way of changing the format of the date to check, as it is attached to a post, and I don't know what posts to get out of the database until I can check if they are between my chosen dates... 我无法更改要检查的日期的格式,因为它附加到帖子,我不知道从数据库中删除哪些帖子,直到我可以检查它们是否在我选择的日期之间...

Any ideas if this is possible, or am I looking at two queries (grab all the posts get the IDs of the ones that I actually want by checking the data in PHP)? 任何想法,如果这是可能的,或者我正在看两个查询(抓住所有帖子通过检查PHP中的数据获取我真正想要的ID)? Thanks. 谢谢。

The way to convert your date would be to use str_to_date : 转换日期的方法是使用str_to_date

STR_TO_DATE(event_date, '%d/%m/%Y')

This converts your date to something you can use in your BETWEEN formula. 这会将您的日期转换为您可以在BETWEEN公式中使用的日期。

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_str-to-date

You can use the following construction: 您可以使用以下构造:

STR_TO_DATE(postmeta.event_date, '%d/%m/%Y') BETWEEN 'yyyy-mm-dd' AND 'yyyy-mm-dd' + INTERVAL 28 DAY STR_TO_DATE(postmeta.event_date,'%d /%m /%Y')在'yyyy-mm-dd'和'yyyy-mm-dd'+ INTERVAL 28天之间

However using a custom date format, such that is not supported natively by the database, in a column that can used for filtering is commonly a bad design, unless maybe one knows exactly what they are doing. 但是,使用自定义日期格式(数据库本身不支持),在可用于过滤的列中通常是一个糟糕的设计,除非可能确切知道他们在做什么。

A query relying on the filter provided above will work, but it won't be very efficient as such range lookup cannot be optimized through indexes. 依赖于上面提供的过滤器的查询将起作用,但由于无法通过索引优化此类范围查找,因此效率不高。

How about 怎么样

WHERE event_date BETWEEN 'yyyy-mm-dd' AND DATE_ADD('yyyy-mm-dd', INTERVAL 28 DAY)

Obviously the yyyy-mm-dd will be replaced with actual values: 显然yyyy-mm-dd将被替换为实际值:

WHERE event_date BETWEEN '2011-05-11' AND DATE_ADD('2011-05-11', INTERVAL 28 DAY)

If your date string is not in the expected format, you'll have to parse it first. 如果您的日期字符串不是预期的格式,则必须先解析它。 You can do that in PHP or use STR_TO_DATE . 您可以在PHP中执行此操作或使用STR_TO_DATE

WHERE event_date 
BETWEEN 
    STR_TO_DATE('01/05/2011','%d/%m/%Y') AND
    DATE_ADD(STR_TO_DATE('01/05/2011','%d/%m/%Y'), INTERVAL 28 DAY)

Here is period from today - to the next 28 days 这是从今天到接下来的28天

  $now = mktime();
  //set your period date
  $from = mktime(0, 0, 0, date("m",$now), date("d",$now), date("Y",$now));
  $to   = mktime(23, 59, 59, date("m",$now), date("d",$now)+28, date("Y",$now));

HERE IS YOUR QUERY 这是你的查询

 SELECT * FROM `postmeta` WHERE UNIX_TIMESTAMP(`event_date`)>='".$from."' AND UNIX_TIMESTAMP(`event_date`)<='".$to."'

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

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