Optimizing the time display _php skills of articles and comments in WordPress

Source: Internet
Author: User
Tags abs diff sprintf time limit

Many bloggers like to use comments in "XXX minutes Ago", article published in "XXX minutes ago" to show the time of the article comments, improve the time display not only can be very intuitive to tell the reader this article or comment on how long it has been, more can enhance the sense of time of comment reply, brother very much like, Because there are so many things that have been pressed in my hand for a while, weekday day and suffer from not surfing the internet, so the theme of the style and functional part of the 1.1-point writing dragged for a long time, recently it is just round to toss comments, so the gradual reference to the online popular style 1.1 points of modification of their own comment style and function.

So .....
Go .....
Dealing with sacks .... Comment Date and article date call function different, below the comment date as an example, the article date please adjust.

The principle of improved time display mode
Very simple, it is through a built-in WordPress function to deal with the current time and article, comment on the timing of the publication, showing that there are X minutes, x hours, x days.
This function is Human_time_diff ()

Usage:

 <?php Human_time_diff ($from, $to); >

Description
Determine the difference between two time markers.
In a human-readable format, such as "1 hours", "5 minutes", "Two Days", returns the time difference between $from and $to two variables.

The English language is also very good understanding: from to. It's a waste, haha. )

Prototype version to improve implementation

 Change the function of your comment time to the following
<?php Echo Human_time_diff (get_comment_time (' U '), Current_time (' timestamp '));? >

All dates are calculated to be a time lag, is it violent?

The primary version of the implementation approach
simply add a judgment, if the comment time is not more than one day, show xx hours before, if more than one day displays the original date.
This is more humane, right? Can't let the reader always count the finger to calculate 38 days ago is what day? Ha ha!
Code:

 <?php// 
calculation is more than one day note: 86400 is the total number of seconds in a day 60 seconds X60 minutes X24 hours =86400 seconds
//If you feel that the day is not enough, please calculate the fill.
if (current_time (' timestamp ')-get_comment_time (' U ') < 86400)
//The things displayed within one day
{$cmt _time = human_time_ diff (Get_comment_time (' U '), Current_time (' timestamp ')). '-ago ';}
More than a day so show
else{$cmt _time = get_comment_date (' y/n/j '). '-'. Get_comment_time (', ', false);
 >


 //change your comment time to the function shown below
<?php echo $cmt _time;? >

Enhanced version
so can we make it any stronger?
Why is it enhanced?
All right, because I am more serious, I think the Chinese display date is not good-looking, affect my typesetting, like the English display date, and the Chinese version of the WordPress Han is really no corner (Chinese really careful), if we directly with the Human_time_diff function output, the Chinese version of the WordPress will be all the results of the Chinese display xx hours ago, this is likely to affect our layout, and this human_time_diff function has neither left the hook, nor reserved a not the parameters of the Han, so we want to display English words, there are only two ways:

Directly modify the Human_time_diff function, let the Chinese invalid, so do too violent, and later upgrade also want to go in to change, Roentgen home does not like.
Rewrite one's own Human_time_diff function, bypassing the Chinese.
Function.php inside the strong insert the following code:

 The original function of day hour min are lowercase,
//I put the first letter of these three words to uppercase, that day hour min can avoid the Han, you understand?
if (! function_exists (' Xz_time_diff ')):
function Xz_time_diff ($from, $to = ') {
 if (empty ($to)) 
   
     $to = time ();
 $diff = (int) abs ($to-$from);
 if ($diff <= 3600) {
 $mins = round ($diff/60);
 if ($mins <= 1) {
  $mins = 1;
 }
 /* Translators:min=minute/
 $since = sprintf (_n ('%s min ', '%s mins ', $mins), $mins);
 } else if (($diff <= 8 6400) && ($diff > 3600)) {
 $hours = round ($diff/3600);
 if ($hours <= 1) {
  $hours = 1;
 }
 $since = sprintf (_n ('%s Hour ', '%s Hours ', $hours), $hours);
 } ElseIf ($diff >= 86400) {
 $days = round ($diff/86400);
 if ($days <= 1) {
  $days = 1;
 }
 $since = sprintf (_n ('%s day ', '%s days ', $days), $days);
 }
 return $since;
} endif


   

The time judgement code should read as follows:

 <?php 
//only to calculate the date difference of the function name changed only, other ibid.
if (current_time (' timestamp ')-get_comment_time (' U ') < 86400)
{$CMT _time = Xz_time_diff (Get_comment_time (' U '), Current_time (' timestamp ')). '-ago ';}
else{$CMT _time = get_comment_date (' y/n/j '). '-'. Get_comment_time (', ', false);
 >


 //change your comment time to the function shown below
<?php echo $cmt _time;? >

Show comments, article relative time

According to the above version, the following this should be an enhanced version of it, because to achieve the effect or need to add code in the subject, so not to the ultimate version, haha.
The function code is as follows:

Relative Time function

if (! function_exists (' Xz_time ')):/** * Displays the package function for the article and comment relative time.
 * Author: Xiangzi http://PangBu.com/* @param $type type string ' CMT ' or ' art ' to define whether the comment time or the article time is displayed.
 * @param $ago _time Numeric type to define the time limit for displaying relative time defaults to 86,400 seconds that day. * @param $after string display text after the relative time, default to '-ago ' * @param $late the item that is displayed after the string exceeds the time limit, default to Get_the_time (' y/n/j-h:i ') or Get_comme Nt_time (' y/n/j-h:i ') * @return return string (relative time or absolute time)/function xz_time ($type = ' Art ', $ago _time = 86400, $after = '-AG
      O ', $late = ') {if ($type = = ' cmt ') {$diff = (int) abs (get_comment_time (' U ')-current_time (' timestamp '));
  if (! $late) | | | | $late = = ') {$late = Get_comment_time (' y/n/j-h:i ');
    } if ($type = = = ' Art ') {$diff = (int) abs (get_the_time (' U ')-current_time (' timestamp '));
  if (! $late) | | | | $late = = ') {$late = Get_the_time (' y/n/j-h:i ');
    } if ($diff <= 3600) {$mins = round ($diff/60);
    if ($mins <= 1) {$mins = 1; }/* Translators:min=minute */$since = sprintf (_n ('%s Min ', '%s mins ', $mins), $mins);
    else if (($diff <= 86400) && ($diff > 3600)) {$hours = round ($diff/3600);
    if ($hours <= 1) {$hours = 1;
  $since = sprintf (_n ('%s Hour ', '%s Hours ', $hours), $hours);
    } elseif ($diff >= 86400) {$days = round ($diff/86400);
    if ($days <= 1) {$days = 1;
  $since = sprintf (_n ('%s day ', '%s days ', $days), $days);
  }; 
  $since. = $after; Return $diff < $ago _time?
$since: $late;
 }endif;

How to use
Insert the above code into your subject's function.php file
Then call the function where you want to show the relative time.
function minimum input set an argument that $type type string ' CMT ' (Comment time) or ' art ' (article time)
Example:

 The simplest invocation of the
Echo xz_time (' CMT ');
Output in one day: 3 Hours-ago 
//day output: 2015/12/26-20:01
 
 
//Call time is 2 days relative time, before time shows the default time
echo xz_time (' CMT ', 172800);
Output within 2 days: 3 Hours-ago 
//2 days after the output: 2015/12/26-20:01
 
 
//Call time is 2 days relative time, relative time after the "comment"
echo xz_time (' CMT ', 172800, ' before comment ');
Output within 2 days: 3 Hours before comments 
//2 days Output: 2015/12/26-20:01
 
//Call time is 2 days relative time, before the time is displayed as the year-month-day
Echo xz_time (' CMT ', 172800, ' before comment ', get_comment_time (' y-n-j '));
Output within 2 days: 3 Hours Previous comments 
//2 days after output: 2015/12/26

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.