开发者

Checking if the UNIX timestamp stored in MySQL DB is this weeks or this months?

开发者 https://www.devze.com 2023-04-07 04:09 出处:网络
I have a MySQL DB, and in one of the tables I have stored the time in which the content was submitted its in the form of a UNIX timestamp, the column is called content_time. Below are two pseudo examp

I have a MySQL DB, and in one of the tables I have stored the time in which the content was submitted its in the form of a UNIX timestamp, the column is called content_time. Below are two pseudo examples of queries to demonstrate what I'm trying to accomplish, just not sure how to go by doing this (although I understand I will need do some some comparisons between the current and stored timestamps within the WHERE clause):

SELECT * FROM content 
WHERE content_time = THIS WEEKS 
开发者_如何转开发(the content was posted at any time/day within the current week)

SELECT * FROM content 
WHERE content_time = THIS MONTHS 
(the month and year from content_time match with the current)

Appreciate all help.


See MySQL's Date and Time Functions, specifically FROM_UNIXTIME(), WEEK() and MONTH(). Keep in mind that when checking is it the same week or month you probably also want to check is it the same year.

Another option is to generate start and end timestamps for the time range youre intrested in (ie timestamp for the beginning of the week and for the end of the week) and then use WHERE(content_time BETWEEN start AND end)

0

精彩评论

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