Jump to content

Explained myself alot better on this Topic


Monkuar

Recommended Posts

Ok I found the culprit and it's easy!

 

function  timeAgo2($timestamp, $granularity=3, $format='d:H:i:s'){
        $difference = $timestamp;
        if($difference < 0) return '0 seconds ago';
        elseif($difference < 864000){
                   $periods = array('week' => 604800,'day' => 86400,'hours' => 3600,'min' => 60,'seconds' => 1);
                $output = '';
                foreach($periods as $key => $value){
                        if($difference >= $value){
                                $time = round($difference / $value);
                                $difference %= $value;
                                $output .= ($output ? ' ' : '').$time.' ';
                                $output .= (($time > 1 && $key == 'day') ? $key.'s' : $key);
                                $granularity--;
                        }
                        if($granularity == 0) break;
                }
                return ($output ? $output : '0 seconds').'';
        }
        else return date($format, $timestamp);
}

 

 

Now I will use:

 

timeAgo2(136622,5)

 

Which works and it will output:

2 days 14 hours 57 min 2 seconds

 

 

But, if I add another "1" to it like this:

 

timeAgo2(1136622,5)

 

 

It shows:

14:03:43:42

 

Which is not right, it needs to show, 14 Days, 3hours 43minutes and 42 seconds.... Thank you, I finally explained myself better here, hope you understand

timeAgo2(1136622,5)

 

 

It shows:

14:03:43:42

 

Which is not right, it needs to show, 14 Days, 3hours 43minutes and 42 seconds.... Thank you, I finally explained myself better here, hope you understand

You do realise it is showing what you want, right?

 

EDIT: strike that, I see what you mean...

 

EDIT: With your 'if' statement, with that second example you're showing, $difference is greater than 864000, so that 'if' statement never executes for... timeAgo2(1136622,5)

        elseif($difference < 864000){

 

1136622 is > than 864000, so it skips your code and goes down to the date() call with the format string.  The problem here is that your number is not a timestamp, it's a duration so your formatted date is not going to be what you want.  You need to either remove your limit on the duration, or rework your function to accept a timestamp value so that your date() call returns the correct result.

 

        elseif($difference < 864000){

 

1136622 is > than 864000, so it skips your code and goes down to the date() call with the format string.  The problem here is that your number is not a timestamp, it's a duration so your formatted date is not going to be what you want.  You need to either remove your limit on the duration, or rework your function to accept a timestamp value so that your date() call returns the correct result.

 

Wow, could not believe I did not see that, Thank you "MasterACE14" for your reply also.

 

this Code works fine for what I need, I am displaying a "time left" for a user who got suspended, so if it's:

 

timeAgo2(19236622,5)

 

it will show the user:

 

32 week 6 days 16 hours 30 min 22 seconds

 

 

But one problem, I need to add months to this function, would I just simply add month into that array with the corresponding seconds?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.