【问题标题】:How to change date to days ago (human time diff)如何将日期更改为几天前(人类时间差异)
【发布时间】:2018-01-02 11:38:24
【问题描述】:

我正在使用 WordPress 主题报纸。现在日期显示为“2017 年 7 月 26 日”,但我希望它显示为“4 小时前”或“3 天前”。环顾四周后,我发现有一个 WordPress 代码可以做到这一点。但是,我不知道如何用人类时间差异替换我网站的当前日期。有人有什么建议吗?

我的主题使用的获取日期功能:

    function get_date($show_stars_on_review = true) {
        $visibility_class = '';
        if (td_util::get_option('tds_m_show_date') == 'hide') {
            $visibility_class = ' td-visibility-hidden';
        }

        $buffy = '';
        if ($this->is_review and $show_stars_on_review === true) {
            //if review show stars
            $buffy .= '<div class="entry-review-stars">';
            $buffy .=  td_review::render_stars($this->td_review);
            $buffy .= '</div>';

        } else {
            if (td_util::get_option('tds_m_show_date') != 'hide') {
                $td_article_date_unix = get_the_time('U', $this->post->ID);
                $buffy .= '<span class="td-post-date">';
                    $buffy .= '<time class="entry-date updated td-module-date' . $visibility_class . '" datetime="' . date(DATE_W3C, $td_article_date_unix) . '" >' . get_the_time(get_option('date_format'), $this->post->ID) . '</time>';
                $buffy .= '</span>';
            }
        }

        return $buffy;
    }

函数的调用位置:

    <div class="item-details">
        <?php echo $this->get_title();?>
        <div class="td-module-meta-info">
            <?php if (td_util::get_option('tds_category_module_mx1') == 'yes') { echo $this->get_category(); }?>
            <span class="td-author-date">
                <?php echo $this->get_author();?>
                <?php echo $this->get_date();?>
            </span>
        </div>
        <div class="td-excerpt">
            <?php echo $this->get_excerpt();?>
        </div>
    </div>

人类时间差异代码:

<?php echo str_replace('mins', 'minutes', human_time_diff( get_the_time('U'), current_time('timestamp') ) . ' ago'); ?>

我要更改日期的元素

<?php

class td_module_2 extends td_module {

    function __construct($post) {
        //run the parrent constructor
        parent::__construct($post);
    }

    function render() {
        ob_start();
        ?>

        <div class="<?php echo $this->get_module_classes();?>">
            <div class="td-module-image">
                <?php echo $this->get_image('td_324x160');?>
                <?php if (td_util::get_option('tds_category_module_2') == 'yes') { echo $this->get_category(); }?>
            </div>
            <?php echo $this->get_title();?>


            <div class="td-module-meta-info">
                <?php echo $this->get_author();?>
                <?php echo wp_relative_date(get_the_time('U'));?>
            </div>


            <div class="td-excerpt">
                <?php echo $this->get_excerpt();?>
            </div>

            <?php echo $this->get_quotes_on_blocks();?>

        </div>

        <?php return ob_get_clean();
    }
}

【问题讨论】:

    标签: php html wordpress function date


    【解决方案1】:

    在functions.php文件中添加

    function wp_relative_date($post_date)
    {
        return human_time_diff( $post_date , current_time( 'timestamp' ) ) . ' ago';
    }
    

    要显示时间,请在循环中添加这个

    echo wp_relative_date(get_the_time('U'));
    

    【讨论】:

    • 我将如何专门将其添加到我的主题中?我有上面的代码(在项目详细信息类中)
    • 你可以简单地将第一部分放在functions.php文件的类之外。是在类还是命名空间下?
    • 我将更改日期的元素的代码放在我的答案中。看起来对吗?是说所有日期都是 6 天前,即使它们是 10 天前或 10 小时前?
    • 你觉得是不是因为主题已经有了get_date这个函数?您认为我只需要编辑该代码吗? (我上面问题中的第一个代码)
    【解决方案2】:

    这里有一些代码可以根据设置的当前日期和时间转换为分钟、小时或天,发布日期是您创建元素时的日期时间戳。

    试试看这是否符合你的要求。

    $post_date 更改为创建帖子的时间戳。 (例如,在下面的示例中,基于我发布此答案的时间显示 1 天...)

    将下面的代码包装在一个函数中,并使用带有时间戳('7/25/2017 00:00:00')的 wordpress getDate 功能传入 $post_date,该函数将返回小时、分钟、天。

    $seconds_hours = 60*60;
    $hours_days = $seconds_hours*24;
    
    $today = date("Y-m-d");
    $timestamp = $today.' '.date("H:i:s");
    
    $post_date = '7/25/2017 00:00:00';
    
    $difference = strtotime($timestamp)-strtotime($post_date);
    if ($difference>$hours_days) {
        $date1 = new DateTime(substr($post_date,0,10));
        $date2 = new DateTime($today);
        $since = $date2->diff($date1)->format("%d");
        if ($since==1) { $since .= ' day'; }
        else { $since .= ' days'; }
    } else if ($difference>$seconds_hours) {
        $since = floor($difference/$seconds_hours).' hours';
    } else {
        $since = floor($difference/60).' mins';//mins
    }
    
    echo $since;
    

    Wordpress 解决方案(未经测试)

    function human_difference($post_date) {
        $seconds_hours = 60*60;
        $hours_days = $seconds_hours*24;
    
        $today = date("Y-m-d");
        $timestamp = $today.' '.date("H:i:s");
    
        $difference = strtotime($timestamp)-strtotime($post_date);
        if ($difference>$hours_days) {
            $date1 = new DateTime(substr($post_date,0,10));
            $date2 = new DateTime($today);
            $since = $date2->diff($date1)->format("%d");
            if ($since==1) { $since .= ' day'; }
            else { $since .= ' days'; }
        } else if ($difference>$seconds_hours) {
            $since = floor($difference/$seconds_hours).' hours';
        } else {
            $since = floor($difference/60).' mins';//mins
        }
    
        return $since;
    }
    
    $post_date_time = get_the_date() . ' ' . get_the_time(); //Format this to m/d/Y H:i:s
    echo human_difference($post_date_time); // x days or x hours or x minutes
    

    【讨论】:

    • 感谢您的帮助。我收到了这个错误 - 解析错误:语法错误,意外的 '$post_date_time' (T_VARIABLE),在这一行上期待函数 (T_FUNCTION) - $post_date_time = get_the_date() 。 ' ' 。 get_the_time(); //将此格式化为m/d/Y H:i:s
    • 确保以正确的格式添加到 get_the_date 和 time。 EG get_the_date('m/d/Y') 等
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 2021-04-07
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多