Jump to content

[SOLVED] Date queries


thaxroads

Recommended Posts

I have a cms site I'm making and I want to add a section in the admin area where it shows how many users joined:

 

TODAY

YESTERDAY

THIS MONTH

LAST MONTH

THIS YEAR

LAST YEAR

 

The queries I have so far:

 

$query="SELECT count(*) FROM users WHERE joindate=now()-INTERVAL 24 HOUR"; // TODAY

$query="SELECT count(*) FROM users WHERE joindate=now()-INTERVAL 1 DAY"; // YESTERDAY

$query="SELECT count(*) FROM users WHERE YEAR(joindate)=YEAR(now()) AND MONTH(joindate)=MONTH(now())";

$query="SELECT count(*) FROM users WHERE YEAR(joindate)=YEAR(now()-INTERVAL 1 MONTH) AND 
MONTH(joindate)=MONTH(now()-INTERVAL 1 MONTH)";

$query="SELECT count(*) FROM users WHERE YEAR(joindate)=YEAR(now())";

$query="SELECT count(*) FROM users WHERE YEAR(joindate)=YEAR(now()-INTERVAL 1 YEAR)";

 

of course the first two codes which are today & yesterday queries do not work ... the dates are in the MySQL database in this format: 0000-00-00 00:00:00 ... can someone help me out with the correct queries I'm looking for ..

 

Any help would be appreciated

 

Thanks

 

Link to comment
Share on other sites

Try using CURDATE, this should get you yesterday

 

SELECT * FROM users
WHERE DATE( joindate ) <= DATE_ADD( CURDATE( ) , INTERVAL -1
DAY ) 
ORDER BY joindate DESC

 

 

Thanks ... this is what I came up with after your input about CURDATE():

 

$query="SELECT count(*) FROM users WHERE DATE(joindate)=CURDATE()"; //USER JOINED TODAY
$query="SELECT count(*) FROM users WHERE DATE(joindate)=DATE_ADD(CURDATE(), INTERVAL -1 DAY)"; // USERS JOINED YESTERDAY

 

Works like a charm!

 

Thanks Again!! ;D

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.