Php - wordpress中的human_time_diff

时间:2013-12-04 23:30:06

标签: php wordpress

所以我使用php函数以自定义方式回显帖子的日期。

$last_modified = human_time_diff( get_the_date('U','','', false), current_time('timestamp') );

in my localhost offline this gives me:
11/16/2013 -> 18 days

but in the online website where I need it correct:
11/16/2013 -> 3 weeks

由于我计算显示自定义日期格式的日期,而不是几天,在线版本计算周数。这可能是什么原因引起的?我检查了两个数据库,它们看起来像日期格式等。可以是不同的PHP版本?谢谢

1 个答案:

答案 0 :(得分:2)

这是wordpress的不同版本的问题。 3.5.1中的函数human_time_diff与3.7.1中的相同。只需查看各种版本的源代码:

要解决此问题,您需要使用一些PHP知识。有几种方法有自己的缺点和优点:

  1. 您可以在新版本中反向移植旧版本的功能 wp-includes/formatting.php,但有其他名称 像这样human_time_diff_351()。但 因此,您需要手动更改代码以使用新代码 通过重命名旧函数的调用来实现。
  2. 您可以通过v.3.5.1中的函数替换新函数,但它会导致一些副作用, 你和我都不确定。
  3. 所以我认为在你的情况下,更改检查一切正常后,最好使用第二种方法测试网站。这是v.3.5.1中的代码:

    function human_time_diff( $from, $to = '' ) {
            if ( empty( $to ) )
                    $to = time();
            $diff = (int) abs( $to - $from );
            if ( $diff <= HOUR_IN_SECONDS ) {
                    $mins = round( $diff / MINUTE_IN_SECONDS );
                    if ( $mins <= 1 ) {
                            $mins = 1;
                    }
                    /* translators: min=minute */
                    $since = sprintf( _n( '%s min', '%s mins', $mins ), $mins );
            } elseif ( ( $diff <= DAY_IN_SECONDS ) && ( $diff > HOUR_IN_SECONDS ) ) {
                    $hours = round( $diff / HOUR_IN_SECONDS );
                    if ( $hours <= 1 ) {
                            $hours = 1;
                    }
                    $since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours );
            } elseif ( $diff >= DAY_IN_SECONDS ) {
                    $days = round( $diff / DAY_IN_SECONDS );
                    if ( $days <= 1 ) {
                            $days = 1;
                    }
                    $since = sprintf( _n( '%s day', '%s days', $days ), $days );
            }
            return $since;
    }
    

    在文件wp-includes/formatting.php v.3.7.1中的第2134-2174行上的注释函数,而不是放置该函数。