How to get updated clock & who's online

Spartak

Well-known member
Member
Joined
9 yrs. 9 mth. 5 days
Messages
587
Reaction score
8,878
Wallet
0$
How to get updated clock & who's online

What You will get is the current time updating every second (this also includes the date) and the users online updating at a rate set by the mybb setting settings-&gt;who'sonline-&gt;cut off time. personally I would change the cutoff time to something less than the default 15 minutes.<br />
<br />
first thing to do is alter a couple of language strings in global.lang.php<br />
<br />
1) welcome_current_time<br />
<br />
from <br />
<pre data-deferred="true" class="block-code line-numbers language-none"><code class="language-none">&lt;strong&gt;Current time:&lt;/strong&gt; {1}</code></pre><br />
to<br />
<br />
<pre data-deferred="true" class="block-code line-numbers language-none"><code class="language-none">&lt;strong&gt;Current time:&lt;/strong&gt; &lt;span id=&quot;clock&quot;&gt;&lt;/span&gt;</code></pre><br />
2) welcome_back<br />
<br />
from<br />
<br />
<pre data-deferred="true" class="block-code line-numbers language-none"><code class="language-none">&lt;strong&gt;Welcome back, {1}&lt;/strong&gt;. You last visited: {2}</code></pre><br />
<br />
to<br />
<br />
<pre data-deferred="true" class="block-code line-numbers language-none"><code class="language-none">&lt;strong&gt;Welcome back, {1}&lt;/strong&gt;. You last visited: &lt;span id=&quot;lastvisit&quot;&gt;{2}&lt;/span&gt;</code></pre><br />
now a template edit find the template index_whosonline<br />
and find the line <br />
<pre data-deferred="true" class="block-code line-numbers language-none"><code class="language-none">&lt;td class=&quot;trow1&quot;&gt;&lt;span class=&quot;smalltext&quot;&gt;{$lang-&gt;online_note}&lt;br /&gt;{$onlinemembers}&lt;/span&gt;&lt;/td&gt;</code></pre><br />
and change it to<br />
<pre data-deferred="true" class="block-code line-numbers language-none"><code class="language-none">&lt;td class=&quot;trow1&quot; id=&quot;wol&quot;&gt;&lt;span class=&quot;smalltext&quot;&gt;{$lang-&gt;online_note}&lt;br /&gt;{$onlinemembers}&lt;/span&gt;&lt;/td&gt;</code></pre><br />
all that's happend with these edits is id's have been added to elements so at this point nothing at all will change.<br />
next create a new file call it scripts.js and place the following in it<br />
<pre data-deferred="true" class="block-code line-numbers language-none"><code class="language-none">jQuery(document).ready(function(){
  
    updateClock();
    wol();
    lastvisit();
   
  
});

 
function updateClock ()
    {
    var currentTime = new Date ( );
    var currentHours = currentTime.getHours ( );
    var currentMinutes = currentTime.getMinutes ( );
    var currentSeconds = currentTime.getSeconds ( );
    var d = new Date();
    var n = d.toLocaleDateString();
    // Pad the minutes and seconds with leading zeros, if required
    currentMinutes = ( currentMinutes &lt; 10 ? &quot;0&quot; : &quot;&quot; ) + currentMinutes;
    currentSeconds = ( currentSeconds &lt; 10 ? &quot;0&quot; : &quot;&quot; ) + currentSeconds;

    // Choose either &quot;AM&quot; or &quot;PM&quot; as appropriate
    var timeOfDay = ( currentHours &lt; 12 ) ? &quot;AM&quot; : &quot;PM&quot;;

    // Convert the hours component to 12-hour format if needed
    currentHours = ( currentHours &gt; 12 ) ? currentHours - 12 : currentHours;

    // Convert an hours component of &quot;0&quot; to &quot;12&quot;
    currentHours = ( currentHours == 0 ) ? 12 : currentHours;

    // Compose the string for display
    var currentTimeString = n+&quot;, &quot;+currentHours + &quot;:&quot; + currentMinutes + &quot;:&quot; + currentSeconds + &quot; &quot; + timeOfDay;
    
    
    $(&quot;#clock&quot;).html(currentTimeString);
   setInterval('updateClock()', 1000);
        
}

function wol()
{

$.ajax({
url: 'ajax.php?action=wol',
  type: 'post',
  success: function(data){
   // Perform operation on return value
  
   $(&quot;#wol&quot;).html(data);
  },
  complete:function(data){
   setTimeout(wol,3000); // check every 3 seconds
 
  }
});
}
function lastvisit ()
{
$.ajax({
url: 'ajax.php?action=lastvisit',
  type: 'post',
  success: function(data){
   // Perform operation on return value
  
   $(&quot;#lastvisit&quot;).html(data);
  
  },
  complete:function(data){
   setTimeout(lastvisit,30000); // check every 30 seconds
 
  }
});
}</code></pre><br />
and save it to the jscripts folder<br />
<br />
edit the headerinclude template then add the following line<br />
<pre data-deferred="true" class="block-code line-numbers language-none"><code class="language-none">&lt;script type=&quot;text/javascript&quot; src=&quot;{$mybb-&gt;asset_url}/jscripts/scripts.js&quot;&gt;&lt;/script&gt;</code></pre><br />
but make sure this is placed <span style="font-weight: bold;" class="mycode_b">after </span>jquery has been loaded.<br />
last thing to do is to create a php file for the jquery code to interact with call it ajax.php and paste the following :-<br />
<pre data-deferred="true" class="block-code line-numbers language-none"><code class="language-php">&lt;?php
define('IN_MYBB', 1);
require_once './global.php';
require_once MYBB_ROOT.'inc/functions_forumlist.php';
require_once MYBB_ROOT.'inc/class_parser.php';
$parser = new postParser;
$lang-&gt;load('index');

switch ($_GET['action']) {
case 'wol':
echo wol();
break;
case 'lastvisit':
echo $lastvisit;
break;
}
function wol(){
global $mybb,$db,$cache,$lang,$templates;
$whosonline = '';
if($mybb-&gt;settings['showwol'] != 0 &amp;&amp; $mybb-&gt;usergroup['canviewonline'] != 0)
{
// Get the online users.
if($mybb-&gt;settings['wolorder'] == 'username')
{
$order_by = 'u.username ASC';
$order_by2 = 's.time DESC';
}
else
{
$order_by = 's.time DESC';
$order_by2 = 'u.username ASC';
}

$timesearch = TIME_NOW - (int)$mybb-&gt;settings['wolcutoff'];

$membercount = $guestcount = $anoncount = $botcount = 0;
$forum_viewers = $doneusers = $onlinemembers = $onlinebots = array();

if($mybb-&gt;settings['showforumviewing'] != 0)
{
$query = $db-&gt;query(&quot;
SELECT
location1, COUNT(DISTINCT ip) AS guestcount
FROM
&quot;.TABLE_PREFIX.&quot;sessions
WHERE uid = 0 AND location1 != 0 AND SUBSTR(sid,4,1) != '=' AND time &gt; $timesearch
GROUP BY location1
&quot;);

while($location = $db-&gt;fetch_array($query))
{
$forum_viewers[$location['location1']] += $location['guestcount'];
}
}

$query = $db-&gt;simple_select(&quot;sessions&quot;, &quot;COUNT(DISTINCT ip) AS guestcount&quot;, &quot;uid = 0 AND SUBSTR(sid,4,1) != '=' AND time &gt; $timesearch&quot;);
$guestcount = $db-&gt;fetch_field($query, &quot;guestcount&quot;);

$query = $db-&gt;query(&quot;
SELECT
s.sid, s.ip, s.uid, s.time, s.location, s.location1, u.username, u.invisible, u.usergroup, u.displaygroup
FROM
&quot;.TABLE_PREFIX.&quot;sessions s
LEFT JOIN &quot;.TABLE_PREFIX.&quot;users u ON (s.uid=u.uid)
WHERE (s.uid != 0 OR SUBSTR(s.sid,4,1) = '=') AND s.time &gt; $timesearch
ORDER BY {$order_by}, {$order_by2}
&quot;);

// Fetch spiders
$spiders = $cache-&gt;read('spiders');

// Loop through all users and spiders.
while($user = $db-&gt;fetch_array($query))
{
// Create a key to test if this user is a search bot.
$botkey = my_strtolower(str_replace('bot=', '', $user['sid']));

// Decide what type of user we are dealing with.
if($user['uid'] &gt; 0)
{
// The user is registered.
if(empty($doneusers[$user['uid']]) || $doneusers[$user['uid']] &lt; $user['time'])
{
// If the user is logged in anonymously, update the count for that.
if($user['invisible'] == 1)
{
++$anoncount;
}
++$membercount;
if($user['invisible'] != 1 || $mybb-&gt;usergroup['canviewwolinvis'] == 1 || $user['uid'] == $mybb-&gt;user['uid'])
{
// If this usergroup can see anonymously logged-in users, mark them.
if($user['invisible'] == 1)
{
$invisiblemark = '*';
}
else
{
$invisiblemark = '';
}

// Properly format the username and assign the template.
$user['username'] = format_name(htmlspecialchars_uni($user['username']), $user['usergroup'], $user['displaygroup']);
$user['profilelink'] = build_profile_link($user['username'], $user['uid']);
eval('$onlinemembers[] = &quot;'.$templates-&gt;get('index_whosonline_memberbit', 1, 0).'&quot;;');
}
// This user has been handled.
$doneusers[$user['uid']] = $user['time'];
}
}
elseif(my_strpos($user['sid'], 'bot=') !== false &amp;&amp; $spiders[$botkey])
{
if($mybb-&gt;settings['wolorder'] == 'username')
{
$key = $spiders[$botkey]['name'];
}
else
{
$key = $user['time'];
}

// The user is a search bot.
$onlinebots[$key] = format_name($spiders[$botkey]['name'], $spiders[$botkey]['usergroup']);
++$botcount;
}

if($user['location1'])
{
++$forum_viewers[$user['location1']];
}
}

if($mybb-&gt;settings['wolorder'] == 'activity')
{
// activity ordering is DESC, username is ASC
krsort($onlinebots);
}
else
{
ksort($onlinebots);
}

$onlinemembers = array_merge($onlinebots, $onlinemembers);
if(!empty($onlinemembers))
{
$comma = $lang-&gt;comma.&quot; &quot;;
$onlinemembers = implode($comma, $onlinemembers);
}
else
{
$onlinemembers = &quot;&quot;;
}

// Build the who's online bit on the index page.
$onlinecount = $membercount + $guestcount + $botcount;

if($onlinecount != 1)
{
$onlinebit = $lang-&gt;online_online_plural;
}
else
{
$onlinebit = $lang-&gt;online_online_singular;
}
if($membercount != 1)
{
$memberbit = $lang-&gt;online_member_plural;
}
else
{
$memberbit = $lang-&gt;online_member_singular;
}
if($anoncount != 1)
{
$anonbit = $lang-&gt;online_anon_plural;
}
else
{
$anonbit = $lang-&gt;online_anon_singular;
}
if($guestcount != 1)
{
$guestbit = $lang-&gt;online_guest_plural;
}
else
{
$guestbit = $lang-&gt;online_guest_singular;
}

$lang-&gt;online_note = $lang-&gt;sprintf($lang-&gt;online_note, my_number_format($onlinecount), $onlinebit, $mybb-&gt;settings['wolcutoffmins'], my_number_format($membercount), $memberbit, my_number_format($anoncount), $anonbit, my_number_format($guestcount), $guestbit);

return $lang-&gt;online_note.'&lt;br&gt;'.$onlinemembers;
}
}
?&gt;
</code></pre><br />
now save ajax.php in your forum root directory <br />
sorry there is so much PHP code but I couldn't find a function in mybb to return the data I needed.<br />
Any questions just ask


Please, Log in or Register to view URLs content!
 
Top Bottom