开发者

Manipulation with date functions in makumba

trying out date functions. Want to list posts only 10 days old, older than that, I won't show. I have this query:

dayOfYear($now)-dayOfYear(p.TS_create)<10 and year($now)=year(p.TS_create)

inside of:

<mak:li开发者_C百科st from="general.forum.Post p" where="dayOfYear($now)-dayOfYear(p.TS_create)<10 and year($now)=year(p.TS_create)">

It works, but just want to ask if there is any better way to do this.


Well this is probably not the best option.

First of all year($now)=year(p.TS_create) will result that on the 1st of Jan you would not see the posts from the last 9 days of the previous years (which I guess you would want to see also).

Otherwise, the dayOfYear() would probably work, but since it represents a day in a year (and not total number of days since min date possible) the better option would be to use something like:

dateAdd(p.TS_create, 10, 'day') > now()

P.S. Also have in mind that if you use $now you have to set it as context attribute, however inside MQL you have a function now() which you can use instead. So if you're not using $now in other places in the page (like in c:if statements) it's better to use the function.


Another option is to use the numerical representation of dates in milliseconds, as in the following:

unix_timestamp(now()) - unix_timestamp(p.TS_create) < 10 * 24 * 60 * 60 * 1000

(10 * 24 * 60 * 60 * 1000 equals to 10 days)

mind that both solutions (using millisecond or dateAdd(p.TS_create, 10, 'day') > now()) will actually compare 240 hours, and disregard calendar days.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜