简体   繁体   English

如何比较php和sql中的日期?

[英]how to compare dates in php and sql?

I want to make a notification system. 我想建立一个通知系统。 Shortly.. to compare two dates, the only problem is that i want to compare the months. 很快..比较两个日期,唯一的问题是我想比较几个月。 to see if a month or two have passed from last notification. 查看上次通知是否已经过了一两个月。

i want to use one or two months from an entry in a mysql database. 我想从mysql数据库中的一个条目使用一到两个月。

the client must select when the notification must come, one or two months. 客户必须选择何时通知,一到两个月。

thank you, Sebastian 谢谢你,塞巴斯蒂安

Your statement should look like this: 您的陈述应如下所示:

SELECT * FROM table WHERE date_column + INTERVAL 1 MONTH >= NOW()

You add 1 (or 2) month to the date of the column in which the date is stored and compare it to the current time "NOW()". 您将1(或2)个月添加到存储日期的列的日期,并将其与当前时间“NOW()”进行比较。

That works with DATE, DATETIME and TIMESTAMP columns. 这适用于DATE,DATETIME和TIMESTAMP列。

You can compare months of two datetime columns inside of MySQL. 您可以比较 MySQL内部两个datetime列的月份

The only computer-related shortcoming of saying "a month has passed" is that a "month" is an arbitrary unit of time; 说“一个月过去了”的唯一与计算机有关的缺点是“月”是一个任意的时间单位; some have 30 days, others have 31 and one even has 28. I would suggest using the logic of "30 days has passed" because it is based on a set unit of time, a day, which is 86,400 seconds long (with negligible error of course). 有些人有30天,有些人有31天,有一天有28天。我建议使用“30天过去了”的逻辑,因为它是基于一天的设定单位,一天是86,400秒(误差可以忽略不计)当然)。

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

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