Jump to content

Counting Number of Log Ins


Xtremer360

Recommended Posts

One more question to ask. I'm trying to do a count of total logins that have happened in the last 5 months including the current one so far.

 

My users_logins table has the following structure: users_id, session_id, login_date(datetime), last_activity(datetime).

 

What the end result is going to be is:

 

<tr>

    <td></td>

    <th scope="col"><?php echo date("M", strtotime("-4 month")); ?></th>

    <th scope="col"><?php echo date("M", strtotime("-3 month")); ?></th>

    <th scope="col"><?php echo date("M", strtotime("-2 month")); ?></th>

    <th scope="col"><?php echo date("M", strtotime("-1 month")); ?></th>

    <th scope="col"><?php echo date("M"); ?></th>

</tr>

<tr>

    <th scope="row">Logins</th>

    <td>94</td>

    <td>53</td>

    <td>124</td>

    <td>92</td>

    <td>105</td>

</tr>

 

Where the first td would be May and the last td would be September. Do I have to do this in 4 separate queries. If so here's what I have for the first query. I'm not sure what woudl go for the WHERE.

 

$four_months_ago_query = "SELCT COUNT(date_login) FROM users_logins WHERE ? ";

Link to comment
https://forums.phpfreaks.com/topic/246758-counting-number-of-log-ins/
Share on other sites

I tried that code but lets say I change it to do:

 

SELECT COUNT( login_date )
FROM users_logins
WHERE login_date >= NOW( ) - INTERVAL 5
DAY
GROUP BY DAY( login_date )

 

Because after thinking about ti I want it to display the last 5 days instead. The query works as well. However since I've displaying the last 5 days how can I get it to show "0" for the last 2 days since there won't be any data for those days yet.

 

 

you'll need to group by year, month, day - otherwise you'll get logins from the 2nd Jan and 2nd Feb and 2nd March etc being counted as one...

for those days with no logins I would just use PHP to determine if that date had no logins, display zero...

What you really need to do is take a giant stick and shove it up your grande ass.. I mean :confused: uh.. I mean..

 

 

$homo= "SELECT * FROM users_logins WHERE login_date BETWEEN NOW() AND DATE_SUB(login_date , INTERVAL 5 day)"; 
$lesbians = mysql_query($homo) or die(mysql_error());

$totals = array();
while($yourMom = mysql_fetch_array($lesbians)){
      $z = 0;
      while($x=1;$x < 5;$++)
      {

          $compareTimes = strtotime(date('Y-m-d H:i:s') . ' +1 day');
          if(strtotime($yourMom['login_date']) <= strtotime('-".$x." day"))
          {
             $z++;
          }
      }
  array_push($totals, $z);
}


foreach($totals as $counted)
{
     echo $counted."<br>";
}

 

now will this work? I have no idea.. just kinda came up on this in a whim its a bit sloppy but it could work..

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.