MyBB Community Forums

Full Version: Time Spent Online on Postbit [Plugin]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
(2021-02-22, 09:07 AM)Crazycat Wrote: [ -> ]The $post must be passed by reference : function timeonline_postbit(&$post)

And this plugin will occure a lot of warning because it uses undefined constants (W, M, D, ...).
BTW, I think it could be higly improved.

Edit :
I did a modification of the fl_nice_time function. It uses the global translation from MyBB.
Now it takes 3 arguments:
  • timeonline : as before, unchanged
  • short : if setted to 1, then the function uses the short naming (d for days, m for months, ...) else it uses the full naming
  • prec : the number of max items you want to display

function fl_nice_time($timeonline, $short=1, $prec=3)
{
	global $lang;
	$start = DateTime::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00');
	$spent = new DateTime('@'.$timeonline);
	$interval = $spent->diff($start);
	$plural = function($nb,$str, $short=0) {
		global $lang;
		if ($short==1) { $suf = '_short'; } else { $suf = ''; }
		return $nb>1?$lang->{$str.'s'.$suf}:$lang->{$str.$suf};
	};
	$format = array();
    if($interval->y !== 0) {
        $format[] = '%y '.$plural($interval->y, 'year', $short);
    }
    if($interval->m !== 0) {
        $format[] = '%m '.$plural($interval->m, 'month', $short);
    }
    if($interval->d !== 0) {
        $format[] = '%d '.$plural($interval->d, 'day', $short);
    }
    if($interval->h !== 0) {
        $format[] = '%h '.$plural($interval->h, 'hour', $short);
    }
    if($interval->i !== 0) {
        $format[] = '%i '.$plural($interval->i, 'minute', $short);
    }
    if($interval->s !== 0) {
        if(!count($format)) {
            return $lang->rel_less_than . ' 1 ' . $plural(1, 'minute', $short);
        } else {
            $format[] = '%s '.$plural($interval->s, 'second', $short);
        }
    }
    if(count($format) > 1) {
		$format = implode(', ', array_slice($format, 0, (int) $prec));
    } else {
        $format = array_pop($format);
    }
    return $interval->format($format);
}

@TODO: add settings to the plugin to set short and prec.


Please hel me , because I have no idea what codes to add and where, please help,  if you know

i will paste the code but for me the same, please see if i did well?

please help me !

my code:


<?php
/**
 * Show Time Spent Online on Postbit 1.0
 * Copyright © 2006 DragonFever
 */
 
$plugins->add_hook("postbit", "timeonline_postbit");
 
function timeonline_info()
{
	return array(
		"name"				=> "Shows Time Spent Online on Postbit",
		"description"		=> "Shows Time Spent Online on Postbit.",
		"website"			=> "http://www.dragonfever.info/",
		"author"			=> "DragonFever",
		"authorsite"		=> "http://www.dragonfever.info/",
		"version"			=> "1.0",
		);
}
 
function timeonline_activate()
{
}
 
function timeonline_deactivate()
{
}
 
function timeonline_postbit($post)
{
	global $mybb, $lang, $db;
	$onuid = $post['uid'];
    $queryon = $db->simple_select("users", "timeonline", "uid='$onuid'");
    $memprofile = $db->fetch_array($queryon);
    
    if($memprofile['timeonline'] > 0)
    {
        $post['timeonline'] = fl_nice_time($memprofile['timeonline']);
    }
    else
    {
        $post['timeonline'] = $lang->na;
    }
	$post['user_details'] = "{$post['user_details']}<font class=\"smalltext\">Spent Online: {$post['timeonline']}</font>";
}
// First Lettered nice_time Funtion
function fl_nice_time($timeonline, $short=1, $prec=3)
{
	global $lang;
	$start = DateTime::createFromFormat('Y-m-d H:i:s', '1970-01-01 00:00:00');
	$spent = new DateTime('@'.$timeonline);
	$interval = $spent->diff($start);
	$plural = function($nb,$str, $short=0) {
		global $lang;
		if ($short==1) { $suf = '_short'; } else { $suf = ''; }
		return $nb>1?$lang->{$str.'s'.$suf}:$lang->{$str.$suf};
	};
	$format = array();
    if($interval->y !== 0) {
        $format[] = '%y '.$plural($interval->y, 'year', $short);
    }
    if($interval->m !== 0) {
        $format[] = '%m '.$plural($interval->m, 'month', $short);
    }
    if($interval->d !== 0) {
        $format[] = '%d '.$plural($interval->d, 'day', $short);
    }
    if($interval->h !== 0) {
        $format[] = '%h '.$plural($interval->h, 'hour', $short);
    }
    if($interval->i !== 0) {
        $format[] = '%i '.$plural($interval->i, 'minute', $short);
    }
    if($interval->s !== 0) {
        if(!count($format)) {
            return $lang->rel_less_than . ' 1 ' . $plural(1, 'minute', $short);
        } else {
            $format[] = '%s '.$plural($interval->s, 'second', $short);
        }
    }
    if(count($format) > 1) {
		$format = implode(', ', array_slice($format, 0, (int) $prec));
    } else {
        $format = array_pop($format);
    }
    return $interval->format($format);
}

// Function Ends
?>
(2021-02-22, 10:03 AM)MyBBEinstein Wrote: [ -> ]Please hel me , because I have no idea what codes to add and where, please help,  if you know

Read what I said:
(2021-02-22, 09:07 AM)Crazycat Wrote: [ -> ]The $post must be passed by reference : function timeonline_postbit(&$post)
Correct the line by adding the & like I show you.

And stop using bold with large police, it's aggressive.
(2021-02-22, 10:19 AM)Crazycat Wrote: [ -> ]
(2021-02-22, 10:03 AM)MyBBEinstein Wrote: [ -> ]Please hel me , because I have no idea what codes to add and where, please help,  if you know

Read what I said:
(2021-02-22, 09:07 AM)Crazycat Wrote: [ -> ]The $post must be passed by reference : function timeonline_postbit(&$post)
Correct the line by adding the & like I show you.

And stop using bold with large police, it's aggressive.


I understand, but I don't know exactly which one I need to fix, so where exactly do I need to insert those codes? or need to change codes, but which one exactly?
Find the line function timeonline_postbit($post)
Replace it with function timeonline_postbit(&$post)

What is not clear ?
(2021-02-22, 11:40 AM)Crazycat Wrote: [ -> ]Find the line function timeonline_postbit($post)
Replace it with function timeonline_postbit(&$post)

What is not clear ?

Ok now super !
thank you


and how can i lowercase non D / M / Y?

this is what my posbit looks like now


[Image: DWgXtyi.png]
Change the line $post['timeonline'] = fl_nice_time($memprofile['timeonline']);
With: $post['timeonline'] = strtolower(fl_nice_time($memprofile['timeonline']));
(2021-02-22, 12:53 PM)Crazycat Wrote: [ -> ]Change the line $post['timeonline'] = fl_nice_time($memprofile['timeonline']);
With: $post['timeonline'] = strtolower(fl_nice_time($memprofile['timeonline']));


I understand and something will change?
Pages: 1 2 3