开发者

PHP working out time since the post

开发者 https://www.devze.com 2023-01-30 19:33 出处:网络
I am trying to create a news feed, and as part of that I am wanting to display to the user how long it has been since the news article was published, something that would look like this,

I am trying to create a news feed, and as part of that I am wanting to display to the user how long it has been since the news article was published, something that would look like this,

Posted: 1 minute and 5 seconds ago

However all I am getting from my function is this,

40 years and 11 months ago

This is what I get whatever the time stamp I send to the function, the function that works out the time since posting,

    function relativeTime($dt,$precision=2)
{
    $times=array(   365*24*60*60    => "year",
                30*24*60*60     => "month",
                7*24*60*60      => "week",
                24*60*60        => "day",
                60*60           => "hour",
                60              => "minute",
                1               => "second");

    $passed=time()-$dt;



    if($passed<5)
    {
        $output='less than 5 seconds ago';
    }
    else
    {
        $output=array();
        $exit=0;
        foreach($times as $period=>$name)
        {
            if($exit>=$precision || ($exit>0 && $period<60))    break;
            $result = floor($passed/$period);
      //die($result);
            if($result>0)
            {
                $output[]=$result.' '.$name.($result==1?'':'s');
                $passed-=$result*$period;
                $exit++;
            }

            else if($exit>0) $exit++;

        }
        $output=implode(' and ',$output).' ago';开发者_如何学运维
    }

//  die($output);
    return $output;
}

$dt = the timestamp something that would look similar to this, 1292534103

In the format tweet function, I am able to get a value from $dt, however when I pass $dt to relativeTime from within the formatTweet function it returns as false, below are both functions,

        function relativeTime($dt,$precision=2)
{
    $times=array(   365*24*60*60    => "year",
                30*24*60*60     => "month",
                7*24*60*60      => "week",
                24*60*60        => "day",
                60*60           => "hour",
                60              => "minute",
                1               => "second");

    $passed=time()-$dt;



    if($passed<5)
    {
        $output='less than 5 seconds ago';
    }
    else
    {
        $output=array();
        $exit=0;
        foreach($times as $period=>$name)
        {
            if($exit>=$precision || ($exit>0 && $period<60))    break;
            $result = floor($passed/$period);
      //die($result);
            if($result>0)
            {
                $output[]=$result.' '.$name.($result==1?'':'s');
                $passed-=$result*$period;
                $exit++;
            }

            else if($exit>0) $exit++;

        }
        $output=implode(' and ',$output).' ago';
    }

//  die($output);
    return $output;
}

And the formatTweet function

    function formatTweet($company_logo = '', $company_name = 'moovjob', $tweet, $dt)
{
    if(is_string($dt)) $dt=strtotime($dt);

    $tweet=htmlspecialchars(stripslashes($tweet));
  //die(print_r($dt));
    return'
    <li><a href="#"><img class="avatar" src="/media/images/employers/'.$company_logo.'width="48" height="48" alt="avatar" /></a>
    <div class="tweetTxt">
    <strong><a href="#">'.$company_name.'</a></strong> '. preg_replace('/((?:http|https|ftp):\/\/(?:[A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?[^\s\"\']+)/i','<a href="$1" rel="nofollow" target="blank">$1</a>',$tweet).'
    <div class="date">'.relativeTime($dt).'</div>
    </div>
    <div class="clear"></div>
    </li>';
}


If you're using PHP 5.3 or newer, you can take advantage of the new Date and Time classes added to PHP.

Specifically, DateTime has a diff method that returns a DateInterval class, which you can then format.


are you sure that you send a value in $dt ?

40 years and 11 month ago sounds a lot like January 1st 1970, which is the beginning of the epoch time.

time() gives you the number of seconds since 1970 so if $dt=0 you get the result that you see.

Keep me posted after you do that check.

I hope this will help you,

Jerome Wagner

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号