Jump to content

Recommended Posts

Ok, so far all I have been able to do is to query the database and return each value:

 

$result = mysql_query("SELECT date, username, timeid FROM totals");

while ($row = mysql_fetch_array($result)) {
   echo $row{'date'}."".$row{'username'}."
   ".$row{'timeid'}."<br>";
}

 

but i don't want to see each individual entry in the database.  All i need is for it to count how many times a specific piece of data shows up for the current date.  If anyone could help i would greatly appreciate it.

Link to comment
https://forums.phpfreaks.com/topic/230052-counting-sql-data-with-current-date/
Share on other sites

My date is stored as text in the format of m-d-yyyy

 

and i've tried that code and i can't figure out how to pull a specific piece of data, like a specific username in the database.  basically i have the user logged in and i want to know how many times their username pops up with the current date

Storing dates as varchar or text is a bad idea. It makes it very difficult to query based on the date or manipulate the date. If possible, you should consider using the DATE datatype for date columns.

 

However, with things the way they are, you can use DATE_FORMAT to convert the CURRENT_DATE() into a string.

DATE_FORMAT(CURRENT_DATE(), '%c-%e-%Y')

Unless you are using two-digits for the month and day, then it would be

DATE_FORMAT(CURRENT_DATE(), '%m-%d-%Y')

Those would give '1-8-2011' and '01-08-2011', respectively.

 

To get the number of times a particular user shows up, the query would be:

SELECT COUNT(*) FROM totals 
WHERE date = DATE_FORMAT(CURDATE(), '%c-%e-%Y') 
AND username = 'whatever'

 

See the mySql manual for DATE_FORMAT()

 

But, if at all possible, consider changing the datatype for the date column in your database. If you ever need to query for a range of dates, a string date is extremely difficult to use.

Storing dates as varchar or text is a bad idea. It makes it very difficult to query based on the date or manipulate the date. If possible, you should consider using the DATE datatype for date columns.

 

However, with things the way they are, you can use DATE_FORMAT to convert the CURRENT_DATE() into a string.

DATE_FORMAT(CURRENT_DATE(), '%c-%e-%Y')

Unless you are using two-digits for the month and day, then it would be

DATE_FORMAT(CURRENT_DATE(), '%m-%d-%Y')

Those would give '1-8-2011' and '01-08-2011', respectively.

 

To get the number of times a particular user shows up, the query would be:

SELECT COUNT(*) FROM totals 
WHERE date = DATE_FORMAT(CURDATE(), '%c-%e-%Y') 
AND username = 'whatever'

 

See the mySql manual for DATE_FORMAT()

 

But, if at all possible, consider changing the datatype for the date column in your database. If you ever need to query for a range of dates, a string date is extremely difficult to use.

 

Thank you thank you thank you!  and there is only one instance that the date will be queried so i'm not worried about format because there is only one area it would be pulled.  but thank you!

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.