2021-02-22, 10:03 AM
(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 thefl_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
?>