Topic: Alternative format_time

i've just been messing with format_time and i've made a slightly different format for dates that happened today
so that instead of it being "Today Time" its "Today timesinceevent" e.g. "Today, 23 min, 25 sec ago"

//
// Format a time string according to $time_format and timezones
//
function format_time($timestamp, $date_only = false)
{
    global $pun_config, $lang_common, $pun_user;

    if ($timestamp == '')
        return $lang_common['Never'];

    $diff = ($pun_user['timezone'] - $pun_config['o_server_timezone']) * 3600;
    $timestamp += $diff;
    $now = time();

    $date = date($pun_config['o_date_format'], $timestamp);
    $today = date($pun_config['o_date_format'], $now+$diff);
    $yesterday = date($pun_config['o_date_format'], $now+$diff-86400);

    if ($date == $today)
        $date = $lang_common['Today'];
    else if ($date == $yesterday)
        $date = $lang_common['Yesterday'];

    if (!$date_only)
        if ($date == $lang_common['Today'])
        {
            $diff = time() - $timestamp;
            $rest = ($diff % 3600);
            $hours = ($diff - $rest) / 3600;
            $seconds = ($rest % 60);
            $minutes = ($rest - $seconds) / 60;
            if($hours > 1)
                return $date.", $hours hours, $minutes min ago";
            elseif ($hours == 1)
                return $date.", an hour, $minutes min ago";
            elseif ($hours == 0)
                return $date.", $minutes min, $seconds sec ago";
        }
        else
            return $date.' '.date($pun_config['o_time_format'], $timestamp);
    else
        return $date;
}

if its more than one hour it cuts off seconds

Re: Alternative format_time

Cool smile

"Programming is like sex: one mistake and you have to support it for the rest of your life."

Re: Alternative format_time

I have a question... how can I swap around the time and the date... so instead of...
20th Sep 05 18:13

I have...
18:13 - 20th Sep 05

and the reason I posted here. Is because I have this bit of code in my forum as it is a nice touch, I was wondering if changing it round would cause a problem.

Thanks in advance.

Re: Alternative format_time

change

return $date.' '.date($pun_config['o_time_format'], $timestamp);

to

return date($pun_config['o_time_format'], $timestamp).' '.$date;

i think

Re: Alternative format_time

Thanks Connorhd. Works a treat... smile