It is necessary to make the conclusion of the date of comments as follows:

  • if published more than 3 hours ago, then display the date as usual
  • if published less than 3 hours ago, then output "% часов/минут/секунд назад"

Tell me how to implement.

Here are the functions:

 function get_comment_date( $d = '', $comment_ID = 0 ) { $comment = get_comment( $comment_ID ); if ( '' == $d ) $date = mysql2date(get_option('date_format'), $comment->comment_date); else $date = mysql2date($d, $comment->comment_date); /** * Filter the returned comment date. * * @since 1.5.0 * * @param string|int $date Formatted date string or Unix timestamp. * @param string $d The format of the date. * @param WP_Comment $comment The comment object. */ return apply_filters( 'get_comment_date', $date, $d, $comment ); } 

 function get_comment_time($d = '', $gmt = false, $translate = true){ $comment = get_comment(); $comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date; if ('' == $d) $date = mysql2date(get_option('time_format'), $comment_date, $translate); else $date = mysql2date($d, $comment_date, $translate); /** * Filter the returned comment time. * * @since 1.5.0 * * @param string|int $date The comment time, formatted as a date string or Unix timestamp. * @param string $d Date format. * @param bool $gmt Whether the GMT date is in use. * @param bool $translate Whether the time is translated. * @param WP_Comment $comment The comment object. */ return apply_filters('get_comment_time', $date, $d, $gmt, $translate, $comment); } 

Now displayed as:

enter image description here

  • mysql2date in what format date gives? - Invision
  • @Invision dmY - Matvey Kottsov
  • You forgot about H: i. strtotime function strtotime convert to a Unix timestamp, add a condition if 3 hours from the current time () has not passed, then output (return) in часов/минут/секунд назад format. How to get this format walks a lot of examples. Example biznesguide.ru/coding/193.html - Invision
  • one
    The best thing is not to do it on the server. This can not be cached and with the stay of the page at the client gradually grows old. - D-side
  • one
    If you are thinking of doing something on the client, pay attention to momentjs.com . She has both the necessary functionality and Russian language support for dates. - VenZell

1 answer 1

 function _ago($tm, $rcs = 0) { $cur_tm = time(); $dif = $cur_tm - $tm; $pds = array('second', 'minute', 'hour', 'day', 'week', 'month', 'year', 'decade'); $lngh = array(1, 60, 3600, 86400, 604800, 2630880, 31570560, 315705600); for ($v = sizeof($lngh) - 1; ($v >= 0) && (($no = $dif / $lngh[$v]) <= 1); $v--) ; if ($v < 0) $v = 0; $_tm = $cur_tm - ($dif % $lngh[$v]); $no = floor($no); if ($no <> 1) $pds[$v] .= 's'; $x = sprintf("%d %s ", $no, $pds[$v]); if (($rcs == 1) && ($v >= 1) && (($cur_tm - $_tm) > 0)) $x .= time_ago($_tm); return $x; } 

UPD 1: Carbon Install Carbon through the composer :

 { "require": { "nesbot/carbon": "~1.18" } } 

In PHP:

 <?php use Carbon\Carbon; echo Carbon::now()->diffForHumans($article->time);