PHP发布后工作时间

PHP发布后工作时间

本文介绍了PHP发布后工作时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个新闻源,作为其中的一部分,我想向用户展示自从发布新闻文章以来已经有多长时间了,

从我的功能得到的是这个,

这是我得到的任何时间戳我发送到功能,功能,发布后的时间,

  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 b $ b 60 * 60 =>小时,
60 =>分钟,
1 => 第二);

$ passed = time() - $ dt;



if($ passed< 5)
{
$ output ='少于5秒前';
}
else
{
$ output = array();
$ exit = 0;
foreach($ times as $ period => $ name)
{
if($ exit> = $ precision ||($ exit> 0&& $ period< 60) )休息
$ 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('和',$ output)。
}

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

$ dt =时间戳,看起来与此类似, 1292534103



格式为tweet的功能,我可以从 $ dt ,但是当我从formatTweet函数中将 $ dt 传递给relativeTime时,它返回为false,以下是两个函数,

  function relativeTime($ dt,$ precision = 2)
{
$ times = array(365 * 24 * 60 * 60 => ;年,
30 * 24 * 60 * 60 =>月,
7 * 24 * 60 * 60 =>周,
24 * 60 * 60 =>day,
60 * 60 =>小时,
60 =>分钟,
1 =>第二);

$ passed = time() - $ dt;



if($ passed< 5)
{
$ output ='少于5秒前';
}
else
{
$ output = array();
$ exit = 0;
foreach($ times as $ period => $ name)
{
if($ exit> = $ precision ||($ exit> 0&& $ period< 60) )休息
$ 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('和',$ output)。
}

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

格式Tetet函数



函数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 =avatarsrc =/ media / images / employer /'.$ company_logo.'width = 48height =48alt =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 =$ 1rel =nofollowtarget =blank> $ 1< / a>',$ tweet)。
< div class =date>'。relativeTime($ dt)。'< / div>
< / div>
< div class =clear>< / div>
< / li>';
}


解决方案

如果您使用PHP 5.3或更高版本,您可以利用新增的类别PHP。



具体来说,有一个方法返回一个类,然后您可以。


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,

However all I am getting from my function is this,

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.

这篇关于PHP发布后工作时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 16:58