Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
404 views
in Technique[技术] by (71.8m points)

php - Get from database but only last 30 days

I'm using this php to fetch data from my mysql database:

$wpdb->get_var("select count(*) from " . $wpdb->prefix . "newsletter where status='C'"); ?>

You may notice i'm in wordpress so i'm using the $wpdb variable.

This returns all data from the table where status='C'. This is all working fine but I need to only get the data from the past 30 days. I'm not sure if mysql stores data about when this entry was stored but I do have another column called created in the same row as status. This stores the date in the following format:

2011-10-14 15:33:58

Is there any way to use that to update my code to only retreive the data from the past month?

Any advise here is greatly appreciated.

Thanks C

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

There is adddate() function in MySQL wich you can use for this :)

$wpdb->get_var("select count(*) from " . $wpdb->prefix . "newsletter where status='C' and date_format(created,'%Y%m%d')>date_format(adddate(now(),interval -30 day),'%Y%m%d')");

This one is for 30 days back but you can replace the "interval -30 day" with "interval -1 month" or visit dev.mysql where you have all explained.

Hope it helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...