2017-07-26 19 views
1

私はWordPressテーマの新聞を使用しています。今は「2017年7月26日」のように表示されていますが、「4時間前」または「3日前」と表示されたいです。周りを見回した後、私はこれを行うWordPressのコードがあることを発見しました。しかし、私は自分のサイトの現在の日付を人間の時間差分と置き換える方法を理解できません。誰にも何か提案はありますか?日付を日に変更する方法(人間の時間差)

のget日付関数は、私のテーマは使用しています:

関数が呼び出され
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(); 
 
    } 
 
}

答えて

0

ここでは掲載日は、あなたが要素を作成したときの日付のタイムスタンプされた状態で設定された現在の日付と時刻に基づいていずれかの分、時間、または日に変換し、いくつかのコードがあります。

これがあなたの必要条件に合っているか試してみてください。

$ post_dateあなたの投稿作成​​のタイムスタンプに変更してください。 (この回答を投稿したときのこの例では、1日を表示しています)

以下のコードを関数にラップし、タイムスタンプ付きの「$ 7」/ 2017 00:00:00 ')、関数は時、分、日を返します。 functions.phpファイルで

$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 
+0

おかげでこれを追加表示するには

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

を追加助けようとしている。このエラーが発生しました - 解析エラー:予期しない '$ post_date_time'(T_VARIABLE)、この行の関数(T_FUNCTION)を期待しています - $ post_date_time = get_the_date()。 ''。 get_the_time(); //これをm/d/Yにフォーマットしてください。H:i:s – user7548188

+0

get_the_dateと時刻に正しいフォーマットを必ず追加してください。 EG get_the_date( 'm/d/Y')など –

0

時間がループ内のため

echo wp_relative_date(get_the_time('U')); 
+0

私はこれを自分のテーマにどうやって追加しますか?上記のコードを持っています(項目の詳細クラス内) – user7548188

+0

functions.phpファイルのクラスの外に最初の部分を置くだけでいいです。それはクラスまたは名前空間の下にありますか? – Webkix

+0

私は私の答えに上記の日付を変更した要素のコードを配置しました。それは正しいか? 10日前か10時間前であっても、すべての日付は6日前であると言いますか? – user7548188

関連する問題