Wordpress posts and comments are displayed several minutes ago several hours ago

Source: Internet
Author: User
Tags php code

First, add the following code to the topic's functions. php file:

The code is as follows: Copy code

Function timeago ($ ptime ){
$ Ptime = strtotime ($ ptime );
$ Etime = time ()-$ ptime;
If ($ etime <1) return 'gang ';
$ Interval = array (
12*30*24*60*60 => 'years ago ('. date ('Y-m-D', $ ptime ).')',
30*24*60*60 => 'months ago ('. date ('M-D', $ ptime ).')',
7*24*60*60 => 'week before ('. date ('M-D', $ ptime ).')',
24*60*60 => 'Days ago ',
60*60 => 'hour ago ',
60 => 'minute ago ',
1 => 'seconds ago'
);
Foreach ($ interval as $ secs => $ str ){
$ D = $ etime/$ secs;
If ($ d> = 1 ){
$ R = round ($ d );
Return $ r. $ str;
    }
};
    }

How to use the list page and article page:

Applicable

Index. php
Search. php
Tag. php
Single. php
Page. php
Author. php
Category. php

Replace the following with the display time? In the php code block:

The code is as follows: Copy code

Echo 'published in'. timeago (get_gmt_from_date (get_the_time ('Y-m-d G: I: s ')));

How to use the comment area:

Replace the following with the display time, which must be placed in the comment loop:

The code is as follows: Copy code

Echo 'published on'. timeago ($ comment-> comment_date_gmt );

Others

Of course, you can also change the text in the function to meet your needs.

The format of the value transmitted by this function is "11:11:11", as long as the format complies with the line, but wp has a gmt time system, so the above usage is available

Then I found a file on the Internet that is also added to the functions. Php file.

// Display the time format of the newly added article before "several minutes"

The code is as follows: Copy code

Function timeago (){
Global $ post;
$ Date = $ post-> post_date;
$ Time = get_post_time ('G', true, $ post );
$ Time_diff = time ()-$ time;
If ($ time_diff> 0 & $ time_diff <24*60*60)
$ Display = sprintf (_ ('% s ago'), human_time_diff ($ time ));
Else
$ Display = date (get_option ('date _ format'), strtotime ($ date ));

Return $ display;
}

Add_filter ('The _ time', 'timeago '); // This is the call method.

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.