Community discussions

MikroTik App
 
mosesjere
just joined
Topic Author
Posts: 7
Joined: Thu Sep 15, 2005 4:13 pm

Hotspot Uptime format

Tue Feb 21, 2006 10:37 am

I have scheduled a script to daily save stats on hotspot usage, but I would like the uptime to be in the format HH:MM:SS or just in seconds instead of wdhms (week,day,hour,minute,second) format. can anyone assist me in either converting to my prefered format of in a command, if any, that will output in the required format.

Thanks

Moses
 
bokad
Member Candidate
Member Candidate
Posts: 152
Joined: Tue Oct 18, 2005 7:34 pm

Fri Mar 31, 2006 2:03 pm

The below PHP function will take the Mikrotik time format (ex. 5d10h23m14s) and return it as number of seconds. From there it should be easy enough to convert it to hh:mm:ss or whatever else you want. There may even be a built in PHP function that already does it. Check http://www.php.net. Of course, you'll have to run this on a PC and not on the MT itself. Currently it only calculates days, hours, minutes, and seconds but could easily be altered to handle weeks as well.

It starts at the left hand side of the MT output and checks to see if there are any days, if so it reads how many and then strips that part of the MT output off. Checks for hours, etc...

--------------------Beginning of Code------------------

<?php
function UptimeInSeconds($uptime) {

$mark1=strpos($uptime, "d");
$days=substr($uptime, 0, $mark1);
if ($mark1) $uptime=substr($uptime, $mark1 + 1);

$mark1=strpos($uptime, "h");
$hours=substr($uptime, 0, $mark1);
if ($mark1) $uptime=substr($uptime, $mark1 + 1);

$mark1=strpos($uptime, "m");
$minutes=substr($uptime, 0, $mark1);
if ($mark1) $uptime=substr($uptime, $mark1 + 1);

$mark1=strpos($uptime, "s");
$seconds=substr($uptime, 0, $mark1);
if ($mark1) $uptime=substr($uptime, $mark1 + 1);

$total=($days * 86400) + ($hours * 3600) + ($minutes * 60) + $seconds;

return $total;

}
?>

--------------------End of Code------------------
 
mfrater
newbie
Posts: 32
Joined: Wed Aug 14, 2013 2:11 am
Location: Auckland, New Zealand
Contact:

Re: Hotspot Uptime format

Sun May 11, 2014 2:15 pm

Here's how to do it in Router OS scripting.
# Convert Time String with weeks/days/hour:min:sec to just hour:min:sec
# by Mark Frater
# 11-May-2014
:local uptimehms;
:local uptime;
:local days;
:local weeks;
:local hours;
:local daypos;
:set daypos (0);

:set uptime ("1w3d10:51:24");
:put $uptime;

:for pos from=0 to=([:len $uptime] - 8) do={
# Find "w" and the weeks
  :if ([:pick $uptime $pos] = "w") do={
:set weeks ( [:pick $uptime 0 ($pos)] );
:put "weeks = $weeks";
:set daypos ($pos+1);
   }
# Find "d" and days
   :if ([:pick $uptime $pos] = "d") do={
:set days ( [:pick $uptime ($daypos) ($pos)] );
:put "days = $days";
   }
}
# Recalculate hours and add :min:sec
:set hours ([:pick $uptime ([:len $uptime]-8) ([:len $uptime]-6)]);
:put "hours = $hours";
:set uptimehms ([:tostr (($weeks * 24 * 7) + ($days * 24) + $hours)].([:pick $uptime ([:len $uptime]-6) [:len $uptime]]));
:put $uptimehms
 
wencs
just joined
Posts: 10
Joined: Wed Mar 26, 2014 7:14 pm

Re:

Mon Mar 19, 2018 3:07 am

The below PHP function will take the Mikrotik time format (ex. 5d10h23m14s) and return it as number of seconds. From there it should be easy enough to convert it to hh:mm:ss or whatever else you want. There may even be a built in PHP function that already does it. Check http://www.php.net. Of course, you'll have to run this on a PC and not on the MT itself. Currently it only calculates days, hours, minutes, and seconds but could easily be altered to handle weeks as well.

It starts at the left hand side of the MT output and checks to see if there are any days, if so it reads how many and then strips that part of the MT output off. Checks for hours, etc...

--------------------Beginning of Code------------------

<?php
function UptimeInSeconds($uptime) {

$mark1=strpos($uptime, "d");
$days=substr($uptime, 0, $mark1);
if ($mark1) $uptime=substr($uptime, $mark1 + 1);

$mark1=strpos($uptime, "h");
$hours=substr($uptime, 0, $mark1);
if ($mark1) $uptime=substr($uptime, $mark1 + 1);

$mark1=strpos($uptime, "m");
$minutes=substr($uptime, 0, $mark1);
if ($mark1) $uptime=substr($uptime, $mark1 + 1);

$mark1=strpos($uptime, "s");
$seconds=substr($uptime, 0, $mark1);
if ($mark1) $uptime=substr($uptime, $mark1 + 1);

$total=($days * 86400) + ($hours * 3600) + ($minutes * 60) + $seconds;

return $total;

}
?>

--------------------End of Code------------------
//
corrección
<?php
function secondsToTime($seconds) {
$dtF = new \DateTime('@0');
$dtT = new \DateTime("@$seconds");
$dias = $dtF->diff($dtT)->format('%a');
$horas = $dtF->diff($dtT)->format('%h');
$minutos = $dtF->diff($dtT)->format('%i');
$segundos = $dtF->diff($dtT)->format('%s');
$tiempo = "";
$coma = "";
if ($dias>0){
if ($dias>1){$tiempo .= $dias." Dias";}else{$tiempo .= $dias." Dia";}
$coma = ", ";
}
//
if ($horas>0){
if ($horas>1){$tiempo .= $coma.$horas." Horas";}else{$tiempo .= $coma.$horas." Hora";}
$coma = ", ";
}else{
$coma = "";
}
//
if ($minutos>0){
if ($minutos>1){$tiempo .=", ". $minutos." Minutos";}else{$tiempo .= ", ".$minutos." Minuto";}
}else{
$coma = "";
}
//
if ($segundos>0){
if ($segundos>1){$tiempo .= ", ".$segundos." Segundos ";}else{$tiempo .= ", ".$segundos." Segundo ";}
}
return $tiempo;
}

function UptimeInSeconds($uptime) {
//
$mark1=strpos($uptime, "w");
$weeks=substr($uptime, 0, $mark1);
if ($mark1) $uptime=substr($uptime, $mark1 + 1);
//
$mark1=strpos($uptime, "d");
$days=substr($uptime, 0, $mark1);
if ($mark1) $uptime=substr($uptime, $mark1 + 1);
//
$mark1=strpos($uptime, "h");
$hours=substr($uptime, 0, $mark1);
if ($mark1) $uptime=substr($uptime, $mark1 + 1);
$mark1=strpos($uptime, "m");
$minutes=substr($uptime, 0, $mark1);
if ($mark1) $uptime=substr($uptime, $mark1 + 1);
$mark1=strpos($uptime, "s");
$seconds=substr($uptime, 0, $mark1);
if ($mark1) $uptime=substr($uptime, $mark1 + 1);
$total=($weeks * 604800) + ($days * 86400) + ($hours * 3600) + ($minutes * 60) + $seconds;
return secondsToTime($total);

}