• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

getTimeString returning too much

president vankk

Web Developer & AuraOT Owner
Joined
Jul 10, 2009
Messages
5,719
Solutions
9
Reaction score
339
Hi dear friends,

What is the problem with this function below? It is returning a number too high.

Code:
function getTimeString($seconds)
{
    $text = "";
    $days = floor(($seconds / 3600) / 24);
    $hours = floor(($seconds / 3600) % 24);
       $minutes = floor(($seconds / 3600) % 60);
    if ($days != 0) {

        if ($days > 1) {
             $text .= $days . " days";
        } else {
             $text .= "1 day";
        }
    }

    if ($hours != 0) {
        if ($days != 0) {
            $text .= ", ";
        }

        if ($hours > 1) {
            $text .= $hours . " hours";
        } else {
            $text .= "1 hour";
        }
    }

    if ($minutes != 0) {
        if ($days != 0 || $hours != 0) {
            $text .= " and ";
        }

        if ($minutes > 1) {
            $text .= $minutes ." minutes";
        } else {
            $text .= "1 minute";
        }
    }

    return $text;
}

For example: 225 days, 16 hours and 16 minutes

Thanks.
 
PHP:
<html>
   <head>
      <title>Online PHP Script Execution</title>    
   </head>
 
   <body>
    
      <?php
         function getTimeString($seconds)
{
    $text = "";
    $days = floor(($seconds / 3600) / 24);
    $hours = floor(($seconds / 3600) % 24);
       $minutes = floor(($seconds / 3600) % 60);
    if ($days != 0) {

        if ($days > 1) {
             $text .= $days . " days";
        } else {
             $text .= "1 day";
        }
    }

    if ($hours != 0) {
        if ($days != 0) {
            $text .= ", ";
        }

        if ($hours > 1) {
            $text .= $hours . " hours";
        } else {
            $text .= "1 hour";
        }
    }

    if ($minutes != 0) {
        if ($days != 0 || $hours != 0) {
            $text .= " and ";
        }

        if ($minutes > 1) {
            $text .= $minutes ." minutes";
        } else {
            $text .= "1 minute";
        }
    }

    return $text;
}
echo getTimeString(111111);
      ?>
 
   </body>
</html>
Code:
1 day, 6 hours and 30 minutes

I didn't edit anything I am just showing the value I passed to it and the results of it.
 
60 seconds in 1 minute
So you multiply 60 x 60 which gives you an hour in seconds, then you multiply that by 24 which gives you a day, then you multiply that by either 7 for 1 week or 365 for a year 366 for leap year in seconds.

You could write a function like this,
PHP:
function xTime($choice, $multiplier)
{
    $x = 0;
    switch($choice){
        case 'minute':
            $x = 60 * $multiplier;
            break;
        case 'hour':
            $x = 60 * 60 * $multiplier;
            break;
        case 'day':
            $x = 60 * 60 * 24 * $multiplier;
            break;
        case 'week':
            $x = 60 * 60 * 24 * 7 * $multiplier;
            break;
        case 'year':
            $x = $multiplier * 60 * 60 * 24 * (in_array(getdate(time())['year'], array(2016))) ? 366 : 365;
            break;
        default:

    }
    return $x;
}

Then call it
PHP:
echo xTime('hour', 5);
The output
Code:
18000

I passed my function to your code
PHP:
echo getTimeString(xTime('hour', 5));

Your code needs to be re-written :(
Code:
5 hours and 5 minutes
 
Last edited:
Should be:
Code:
$minutes = floor(($seconds / 60) % 60);
However this wont fix the issue of returning too much days, it will fix the minute calculation only. I don't think there is a bug to fix anyway, maybe the online time is in ms instead of s? Try using getTimeString(time/1000)

Or its not what you think it is, maybe its a timestamp and then ofc it wont return correctly.
 
There ya go
PHP:
<html>
   <head>
      <title>Online PHP Script Execution</title>
   </head>

   <body>

      <?php
function xTime($choice, $multiplier)
{
    $x = 0;
    switch($choice){
        case 'minute':
            $x = 60 * $multiplier;
            break;
        case 'hour':
            $x = 60 * 60 * $multiplier;
            break;
        case 'day':
            $x = 60 * 60 * 24 * $multiplier;
            break;
        case 'week':
            $x = 60 * 60 * 24 * 7 * $multiplier;
            break;
        case 'year':
            $x = $multiplier * 60 * 60 * 24 * (in_array(getdate(time())['year'], array(2016))) ? 366 : 365;
            break;
        default:

    }
    return $x;
}

function getTimeString($seconds)
{
    $text = "";
    $days =  xTime('day', 1);
    $days = $days != 0 ? floor($seconds / $days) : 0;
    $hours = xTime('hour', 1);
    $hours = $hours != 0 ? floor($seconds / $hours) : 0;
    $minutes = xTime('minute', 1);
    $minutes = $minutes != 0 ? floor($seconds / $minutes) : 0;
    if($hours != 0 && $minutes != 0){
        $minutes = $minutes % $hours;
    }
    if ($days != 0) {

        if ($days > 1) {
             $text .= $days . " days";
        } else {
             $text .= "1 day";
        }
    }

    if ($hours != 0) {
        if ($days != 0) {
            $text .= ", ";
        }

        if ($hours > 1) {
            $text .= $hours . " hours";
        } else {
            $text .= "1 hour";
        }
    }

    if ($minutes != 0) {
        if ($days != 0 || $hours != 0) {
            $text .= " and ";
        }

        if ($minutes > 1) {
            $text .= $minutes ." minutes";
        } else {
            $text .= "1 minute";
        }
    }

    return $text;
}
?>


   </body>
</html>

Say I put 18062. which is 5 hours and 1 minute.. and 2 seconds
PHP:
echo getTimeString(18062);
Code:
5 hours and 1 minute

It needs a bit of work, but this should get you started :)

I would fix this but I really should be working,

If you are working in a language its always good to check out its home page, people post a lot of example code on php.net.
Maybe this will help.
http://php.net/manual/en/function.time.php
 
Last edited:
There ya go
PHP:
<html>
   <head>
      <title>Online PHP Script Execution</title>
   </head>

   <body>

      <?php
function xTime($choice, $multiplier)
{
    $x = 0;
    switch($choice){
        case 'minute':
            $x = 60 * $multiplier;
            break;
        case 'hour':
            $x = 60 * 60 * $multiplier;
            break;
        case 'day':
            $x = 60 * 60 * 24 * $multiplier;
            break;
        case 'week':
            $x = 60 * 60 * 24 * 7 * $multiplier;
            break;
        case 'year':
            $x = $multiplier * 60 * 60 * 24 * (in_array(getdate(time())['year'], array(2016))) ? 366 : 365;
            break;
        default:

    }
    return $x;
}

function getTimeString($seconds)
{
    $text = "";
    $days =  xTime('days', 1);
    $days = $days != 0 ? floor($seconds / $days) : 0;
    $hours = xTime('hour', 1);
    $hours = $hours != 0 ? floor($seconds / $hours) : 0;
    $minutes = xTime('minute', 1);
    $minutes = $minutes != 0 ? floor($seconds / $minutes) : 0;
    if($hours != 0 && $minutes != 0){
        $minutes = $minutes % $hours;
    }
    if ($days != 0) {

        if ($days > 1) {
             $text .= $days . " days";
        } else {
             $text .= "1 day";
        }
    }

    if ($hours != 0) {
        if ($days != 0) {
            $text .= ", ";
        }

        if ($hours > 1) {
            $text .= $hours . " hours";
        } else {
            $text .= "1 hour";
        }
    }

    if ($minutes != 0) {
        if ($days != 0 || $hours != 0) {
            $text .= " and ";
        }

        if ($minutes > 1) {
            $text .= $minutes ." minutes";
        } else {
            $text .= "1 minute";
        }
    }

    return $text;
}
?>


   </body>
</html>

Say I put 18062. which is 5 hours and 1 minute.. and 2 seconds
PHP:
echo getTimeString(18062);
Code:
5 hours and 1 minute

It needs a bit of work, but this should get you started :)

I would fix this but I really should be working,

If you are working in a language its always good to check out its home page, people post a lot of example code on php.net.
Maybe this will help.
http://php.net/manual/en/function.time.php

Thanks buddy.
 
Back
Top