0

This is pretty basic MySQL, but I have not been able to figure this one out, how to do it correctly..

Example: I have a DB table named "table1" with a list of records of user visitors data. Columns: "ID", "TM" and "IP"

"TM" contains timestamp for when the record is stored.

I have a PHP code where I loop through days from a start date to current day. Like this example:

// Start date
$startdateforarray = '2010-07-21';
// End date
$end_date = date("Y-m-d");

    while (strtotime($startdateforarray) <= strtotime($end_date)) {
        $timestamp = strtotime($startdateforarray);

    //Here I want to run my MySQL Query...

        $startdateforarray = date ("Y-m-d", strtotime("+1 day", strtotime($startdateforarray)));

}

Now, inside the loop I want to make a query to count how many results there are in "table1" for each day.

So the MySQL query should be something like:

"SELECT * FROM table1 WHERE TM = (day of $timestamp)" 

Of course (day of $timestamp) is where I have a problem. I know that this should be pretty simple to do, but I havent found a solution yet..

3
  • You shouldn't do a sql query inside a loop; why not just do one query between startdateforarray and end_date? Commented Aug 26, 2014 at 16:26
  • See this answer stackoverflow.com/a/6024944 you can base yourself on it. Commented Aug 26, 2014 at 16:29
  • I´m aware that this is maybe not the most efficient way of doing things. See this post I made: stackoverflow.com/questions/25512088/… Commented Aug 26, 2014 at 17:48

1 Answer 1

1

Assuming by timestamp you mean Unix Timestamp, you can do

SELECT * FROM table1 WHERE FROM_UNIXTIME(TM,'%Y-%m-%d') =  '2010-07-21'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.