1

我想做 StackOverflow 正在做的事情,这就是说自上一篇文章以来已经过去了多长时间。不过有一个问题,SO 会根据上一篇文章的发布时间显示某些信息——例如,如果一篇文章不到一天前,他们会发布上一篇文章的几小时前;如果帖子不到一小时前,他们会发布几分钟前的帖子,等等。

我正在使用以下格式的 MYSQL DateTime 字段:

2012-09-19 13:28:45

我想将上面的时间NOW与时间进行比较,所以我使用 PHP 的strtotime函数转换了那个时间,并尝试通过我放在一起的函数来比较两个日期(如下)。当然,这可能是最糟糕的方法,但是在阅读了 PHPDateDateTime函数之后,我开始变得非常非常困惑。

function get_date_format($strToTimeString){

        $minute = 60;
        $hour = $minutes * 60;
        $day = $hour * 24;
        $week = $day * 7;
        $month = $week * 4;
        $year = $month * 12;

        $timeNow = strtotime("now");

        $timeDiff = $timeNow - $strToTimeString;

        if($timeDiff > $minute){
            if($timeDiff > $hour){
                if($timeDiff > $day){
                    if($timeDiff > $week){
                        if($timeDiff > $month){
                            if($timeDiff > $year){
                                // Years ago
                            }
                            else{
                                // Months ago
                            }
                        }
                        else{
                            // Weeks ago
                        }
                    }
                    else{
                        // Days ago
                    }
                }
                else
                {
                    // Hours ago
                }
            }
            else{
                // Minutes ago
            }

        }
        else{
            // Seconds ago
        }


    }

有一个更好的方法吗?正如我上面提到的,我在尝试使用时没有运气DateTime->diff

我真的很感激任何帮助。

4

4 回答 4

2

使用DateTime. 这是示例:

$now = new DateTime('now');
$posted = new DateTime($postDateFromDBHere);
$interval = $posted->diff($now);
var_dump($interval);
echo $interval->format('%y-%m-%d %h:%m:%s'); //You can do similar to format your output as you wish.
于 2012-09-28T10:15:19.907 回答
1

使用DateTime然后DateTime:diff检查每个值:

function returnInterval($date){
    $datetime1 = new DateTime($date);
    $datetime2 = new DateTime();
    $diff = $datetime1->diff($datetime2);
    $string = '';
    $pass = '';

    if($diff->y){
        $string .= ($diff->y == 1) ? $diff->y." year" : $diff->y." years";
        $pass = ', ';
    }
    if($diff->m){
        $string .= $pass;
        $string .= ($diff->m == 1) ? $diff->m." month" : $diff->m." months";
        $pass = ', ';
    }
    if($diff->d){
        $string .= $pass;
        $string .= ($diff->d == 1) ? $diff->d." day" : $diff->d." days";
        $pass = ', ';
    }
    if($diff->h){
        $string .= $pass;
        $string .= ($diff->h == 1) ? $diff->h." hour" : $diff->h." hours";
        $pass = ', ';
    }
    if($diff->i){
        $string .= $pass;
        $string .= ($diff->i == 1) ? $diff->i." minute" : $diff->i." minutes";
        $pass = ', ';
    }
    if($diff->s){
        $string .= $pass;
        $string .= ($diff->s == 1) ? $diff->s." second" : $diff->s." seconds";
    }
    $pos = strrpos($string, ',');
    $string = substr_replace($string, ' and ', $pos, 2);
    return $string;
}

echo returnInterval('2012-09-19 13:28:45');
// 8 days, 13 hours, 47 minutes and 44 seconds
于 2012-09-28T10:15:52.463 回答
1

经过长时间的搜索,我得到了一些接近 rottentomatoes 在他们的论坛上使用的东西:

function realTimeSince($time){
    define(MINUTE, 60);
    define(HOUR, 60*60);
    define(DAY, 60*60*24);
    define(MONTH, 60*60*24*30);
    $delta = strtotime(gmdate("Y-m-d H:i:s", time())) - strtotime($time);

    if ($delta < 1 * MINUTE) {
        return $delta == 1 ? "one second ago" : $delta . " seconds ago";
    }
    if ($delta < 2 * MINUTE) {
        return "a minute ago";
    }
    if ($delta < 45 * MINUTE) {
        return floor($delta / MINUTE) . " minutes ago";
    }
    if ($delta < 90 * MINUTE) {
        return "an hour ago";
    }
    if ($delta < 24 * HOUR) {
        return floor($delta / HOUR) . " hours ago";
    }
    if ($delta < 48 * HOUR) {
        return "yesterday";
    }
    if ($delta < 30 * DAY) {
        return floor($delta / DAY) . " days ago";
    }
    if ($delta < 12 * MONTH) {
        $months = floor($delta / DAY / 30);
        return $months <= 1 ? "one month ago" : $months . " months ago";
    } else {
        $years = floor($delta / DAY / 365);
        return $years <= 1 ? "one year ago" : $years . " years ago";
    }
}

所以你可以像这样使用它:

realTimeSince('2012-11-12 00:09:54'); //which can be got from a MySQL TIMESTAMP field

基于http://www.ferdychristant.com/blog//archive/DOMM-7QEFK4稍作修改

于 2013-01-29T15:59:05.937 回答
0

试试下面的功能

 function time_ago($date)
        {
            //echo "ss";
            if (empty($date)) { 
                return "No date provided";
            }      
            $periods = array("sec", "min", "hr", "day", "week", "month", "year", "decade");
            $lengths = array("60","60","24","7","4.35","12","10");
            $now = time();
            $unix_date = strtotime($date);
            // check validity of date
            if (empty($unix_date)) {   
                return "Bad date";
            }
            // is it future date or past date
            if ($now >= $unix_date) {   
                $difference= $now - $unix_date;
                $tense = "ago";
            } else {
                $difference = $unix_date - $now;
                $tense = "from now";
            }
            for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
                $difference /= $lengths[$j];
            }
            $difference = round($difference);

            if ($difference != 1 && $j != 0) {
                $periods[$j].= "s";
            }
            if($difference!=0)
            return "$difference $periods[$j] {$tense}";
            else
            return "a few seconds ago";
        }

或者

function time_elapsed_since ($postedDateTime){

    $time = time() - $postedDateTime; // to get the time since that moment

         $tokens = array (
                      31536000 => 'year',
                      2592000 => 'month',
                      604800 => 'week',
                      86400 => 'day',
                      3600 => 'hour',
                      60 => 'minute',
                      1 => 'second'
                  );

                  foreach ($tokens as $unit => $text) {
                      if ($time < $unit) continue;
                      $numberOfUnits = floor($time / $unit);
                      return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'');
                  }

    }





time_elapsed_since($postedDateTime).' ago';
于 2012-09-28T10:09:17.677 回答