0

can anyone help please. i have this script which converts mysql timestamp from the horrible standard date and puts it in a user friendly way "about 2 seconds ago or about 1 hour ago".

my question is this. it works perfectly apart from, when a user posts content, the most recent content they post echos out " about 0 ago" not giving the time it was posted and it isnt until they post something else after this that they see the time on the one they posted before this.

so im wondering why this is because i cant figure it out, i preferabley want it to say "added just now" as soon as the users posted it, and then carry on as the script says saying added 20 seconds ago, 2 minutes ago etc.

an example is;

(content 1) "posted about 0 ago" (content 2) "posted about 8 minute ago" (content 3) "posted about 5 hours ago"

then content 1 will only every update the time when another piece of content is posted else it wont.

so content 1 should say posted just now, then after a few seconds, post about 5 seconds ago, 1 minute ago, 5 hours ago, 7 days ago etc.

 <div class="board-wall-feeds">        
<div class="social_header">
<?php echo "$profile[2]" ?>'s News & Updates:
</div>        
<?php

$timeline_set = get_timeline();
while ($news = mysql_fetch_array($timeline_set)) { 
echo "

<div class=\"news_feeds_board_text\">{$news['content']}
<div class=\"social_footer\">about ".$t.$labels[$i]." ago<a href=\"../../../delete_news_post.php?to=".$news['id']."</div><div class=\"social_clip\"></div></div></a>";

  $datetime1 = new DateTime();
        $datetime2 = new DateTime ($news['date_added']);
        $interval = $datetime1->diff($datetime2);
        $mdhms = explode('-',$interval->format('%m-%d-%H-%i-%s'));

$labels = Array(' months', ' days', ' hours', ' minutes', ' seconds');
$i = 0;
foreach($mdhms as $t){
  if($t > 0) break;
  $i+=1;
}
        } ?>
        </div>

1 Answers1

3

You can try relative time function for this here it is

function relativetime($ts)
{
    $diff = time() - $ts;
    if($diff == 0)
        return 'now';
    elseif($diff > 0)
    {
        $day_diff = floor($diff / 86400);
        if($day_diff == 0)
        {
            if($diff < 60) return 'just now';
            if($diff < 120) return '1 minute ago';
            if($diff < 3600) return floor($diff / 60) . ' minutes ago';
            if($diff < 7200) return '1 hour ago';
            if($diff < 86400) return floor($diff / 3600) . ' hours ago';
        }
        if($day_diff == 1) return 'Yesterday';
        if($day_diff < 7) return $day_diff . ' days ago';
        if($day_diff < 31) return ceil($day_diff / 7) . ' weeks ago';
        if($day_diff < 60) return 'last month';
        return date('F Y', $ts);
    }
    else
    {
        $diff = abs($diff);
        $day_diff = floor($diff / 86400);
        if($day_diff == 0)
        {
            if($diff < 120) return 'in a minute';
            if($diff < 3600) return 'in ' . floor($diff / 60) . ' minutes';
            if($diff < 7200) return 'in an hour';
            if($diff < 86400) return 'in ' . floor($diff / 3600) . ' hours';
        }
        if($day_diff == 1) return 'Tomorrow';
        if($day_diff < 4) return date('l', $ts);
        if($day_diff < 7 + (7 - date('w'))) return 'next week';
        if(ceil($day_diff / 7) < 4) return 'in ' . ceil($day_diff / 7) . ' weeks';
        if(date('n', $ts) == date('n') + 1) return 'next month';
        return date('F Y', $ts);
    }
}

In this function you have to pass time as $ts, converted by strtotime function.

Can be possible duplicate of PHP: producing relative date/time from timestamps

Community
  • 1
  • 1
Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
  • i have tried your code but it comes up with fatal error can not redeclare relative time – ian dearson Feb 15 '13 at 05:07
  • 2
    change the function name `relativeTime` to another one which you `never used`, as this error comes when `two functions` having `same name`. For demo you can try on http://writecodeonline.com/php copy the function there and write one more line after function is `echo relativeTime(strtotime("-1 hour"));` – Rohan Kumar Feb 15 '13 at 05:16