• 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!
  • 2026 staff recruitment is open! Check it out and consider applying!

[PHP] Need help in converting unix time into days, minutes and seconds.

raf

Active Member
Joined
Jan 10, 2011
Messages
266
Reaction score
42
Location
Warsaw, PL
I'm trying to convert unix time to display days, minutes and seconds.

Unix timestamp: 38668931

Basicly, im trying to add Time spent on OT to website, so pulling timestamp from database (players>nick>onlimetime)
 
Last edited:
I used this function a while ago

http://stackoverflow.com/a/14339355
PHP:
function time_elapsed_string($ptime)
{
    $etime = time() - $ptime;

    if ($etime < 1)
    {
        return '0 seconds';
    }

    $a = array( 365 * 24 * 60 * 60  =>  'year',
                 30 * 24 * 60 * 60  =>  'month',
                      24 * 60 * 60  =>  'day',
                           60 * 60  =>  'hour',
                                60  =>  'minute',
                                 1  =>  'second'
                );
    $a_plural = array( 'year'   => 'years',
                       'month'  => 'months',
                       'day'    => 'days',
                       'hour'   => 'hours',
                       'minute' => 'minutes',
                       'second' => 'seconds'
                );

    foreach ($a as $secs => $str)
    {
        $d = $etime / $secs;
        if ($d >= 1)
        {
            $r = round($d);
            return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
        }
    }
}
 
I used this function a while ago

http://stackoverflow.com/a/14339355
PHP:
function time_elapsed_string($ptime)
{
    $etime = time() - $ptime;

    if ($etime < 1)
    {
        return '0 seconds';
    }

    $a = array( 365 * 24 * 60 * 60  =>  'year',
                 30 * 24 * 60 * 60  =>  'month',
                      24 * 60 * 60  =>  'day',
                           60 * 60  =>  'hour',
                                60  =>  'minute',
                                 1  =>  'second'
                );
    $a_plural = array( 'year'   => 'years',
                       'month'  => 'months',
                       'day'    => 'days',
                       'hour'   => 'hours',
                       'minute' => 'minutes',
                       'second' => 'seconds'
                );

    foreach ($a as $secs => $str)
    {
        $d = $etime / $secs;
        if ($d >= 1)
        {
            $r = round($d);
            return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago';
        }
    }
}
i guess it would work just fine, but it's not accurate - it says that every month has 30 days.
 
and the thing is - im using gesior which is rly fucked up inside - the code is messy and i can't load a page with that piece of code from SO.
 
Alright, you could try something like:

PHP:
$date = new DateTime();
$date->setTimestamp(38668931);
$diff = $date->diff(new DateTime());
echo $diff->format('%y years, %d days, %h hours, %i minutes');
that worked as i expected, but i have no idea why, my characters page is blank when i put it like this

PHP:
$onlinetime = new DateTime();
$onlinetime->setTimestamp(38668931);
$diff = $onlinetime->diff(new DateTime());
    
$bgcolor = (($number_of_rows++ % 2 == 1) ?  $config['site']['darkborder'] : $config['site']['lightborder']);
$main_content .= '<tr bgcolor="' . $bgcolor . '"><td>Total online time:</td><td>' . $diff->format('%y years, %d days, %h hours, %i minutes'); . '</td></tr>';

it worked well before i wrapped it with table tags
 
that worked as i expected, but i have no idea why, my characters page is blank when i put it like this

PHP:
$onlinetime = new DateTime();
$onlinetime->setTimestamp(38668931);
$diff = $onlinetime->diff(new DateTime());
   
$bgcolor = (($number_of_rows++ % 2 == 1) ?  $config['site']['darkborder'] : $config['site']['lightborder']);
$main_content .= '<tr bgcolor="' . $bgcolor . '"><td>Total online time:</td><td>' . $diff->format('%y years, %d days, %h hours, %i minutes'); . '</td></tr>';

it worked well before i wrapped it with table tags

Could you PM me, I'll help you there.
 
Making things too complicated for him.. no offense

Simple way is to use gmdate / date and if you need to parse the string use intval :)

To complicated? I saw this guy what he is capable of, I know he would mange to install carbon if he want.
However, your solution would not work either since it counts in milliseconds and not a timestamp.

We managed to get a solution, just convert the milliseconds to seconds and after it I took some random function from internet.

PHP:
        $seconds = (42893678 / 1000);
        function secondsToWords($seconds)
        {
            /*** return value ***/
            $ret = "";
            /*** get the hours ***/
            $hours = intval(intval($seconds) / 3600);
            if($hours > 0)
            {
                $ret .= "$hours hours ";
            }
            /*** get the minutes ***/
            $minutes = bcmod((intval($seconds) / 60),60);
            if($hours > 0 || $minutes > 0)
            {
                $ret .= "$minutes minutes ";
            }
        
            /*** get the seconds ***/
            $seconds = bcmod(intval($seconds),60);
            $ret .= "$seconds seconds";
            return $ret;
        }

How to get the onlinetime in gesior, you can ask @olszak94 , he did some method in player class.
 
To complicated? I saw this guy what he is capable of, I know he would mange to install carbon if he want.
However, your solution would not work either since it counts in milliseconds and not a timestamp.

We managed to get a solution, just convert the milliseconds to seconds and after it I took some random function from internet.

Did I hurt your / his ego?
Sounds like I did even if you say no, because of the response you gave to the suggestion i made.

Life goes on bud :)
 
Did I hurt your / his ego?
Sounds like I did even if you say no, because of the response you gave to the suggestion i made.

Life goes on bud :)

I just think that you not have to say what is complicated for him, thats what I ment.
Sorry if I sounds like a douch, not on purpose :)

Yeah your suggestion would probably work if he was using timestamps as he said in title / thread.
 

Similar threads

Back
Top