ColdFusion: how to check 15 days past the submitted date?

北城以北 提交于 2019-12-11 05:45:47

问题


I saved several accounts in the database. I want to email the accountsdd that have been expired for 15 days. In another word, if the submitted day (date format: mm-dd-yyyy) is 15 days older from today's date then send an email. How do I do it? Any info is greatly appreciated. Thank you.


回答1:


you would want to use the dateDiff function

<cfif dateDiff('d',submittedDate,now()) GT 15>

If your date is really stored as mm-dd-yyyy instead of a date/time object then you would need to use the createODBCDate function

<cfif dateDiff('d',CreateODBCDate(submittedDate),now()) GT 15>

If you're looking to pull all accounts via a query this would work. This would work on MSSQL

SELECT relevant, columns
FROM myTable
WHERE dateDiff(d,submittedDate,getDate()) > 15



回答2:


Without more details (database type, data types, etectera) this is just an educated guess. But it sounds like you are asking how to find all records that were submitted exactly fifteen days ago, regardless of the time. For example if the date and time now is July 29, 2013 08:49 AM, you want to retrieve all records submitted on July 14, 2013 (any time between 12 midnight and 11:59:59 PM).

A query with a simple range comparison should do the trick. Note: While Matt's suggestion of dateDiff (or your database's version of it) would also work, using functions in that manner often prevent the database from leveraging indexes. So it is preferable to use a more index friendly expression, like:

<cfset fifteenDaysAgo = dateAdd("d", -15, now())>
<cfquery ...>
   SELECT  emailAddress
   FROM    yourTable
   WHERE   submittedDate >= <cfqueryparam value="#fifteenDaysAgo#" cfsqltype="cf_sql_date">
   AND     submittedDate < <cfqueryparam value="#dateAdd('d', 1, fifteenDaysAgo)#" cfsqltype="cf_sql_date">
</cfquery>

Then it is just a matter of using the query results to send out your emails.



来源:https://stackoverflow.com/questions/17800653/coldfusion-how-to-check-15-days-past-the-submitted-date

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!