简体   繁体   English

mysql日期间隔,php日期间隔

[英]mysql date interval, php date interval

I have students table and they must pay every month, I have start_day and end_day . 我有学生桌,他们必须每月付款,我有start_dayend_day

For example: 例如:

start_day = "2018-01-01";

end_day = "2018-03-30";

I want to send an SMS 3 days before pay day. 我想在发薪日前3天发送短信。 I don't know how to fetch those dates in this interval. 我不知道如何在此间隔内获取这些日期。

select user_id from schema.table where end_day between
subdate(current_date, interval 3 day) and subdate(current_date, interval 2 day);

A bad way to do this would be : where date(end_day) = current_date-3 一个糟糕的方法是: where date(end_day) = current_date-3

Why I don't prefer the bad way (although it looks simple): 为什么我不喜欢坏方法(尽管看起来很简单):

  1. Avoid using functions on joins and where clause. 避免在joinswhere子句上使用函数。 This'll prevent indexes (if any) from being used and will give you nightmares in terms of performance. 这将阻止使用索引(如果有),并且会给您带来性能方面的噩梦。
  2. Use only the correct operators with intended datatype. 仅使用具有预期数据类型的正确运算符。 - is an arithmetic operator. -是算术运算符。 It's generally not a good practice to use +/- with dates. 在日期中使用+/-通常不是一个好习惯。 Use the functions intended to manipulate dates. 使用旨在操纵日期的功能。 ( subdate, date_add, date_sub etc) subdate, date_add, date_sub等)

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

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