Adjust the time display according to the freshness of the article, similar to the time display of Ali Baishou

Today, when looking at the Ali Baixiu's website, I found that there is a more intimate function, as shown below, the date of the article release can be different with the freshness of the article, such as today's published articles, the release time is specific to a certain minute, and yesterday's published articles, directly display yesterday, and then further, it will be directly display the date.

time

Personally feel that this way of user experience is very good, so a simple study of the realization of the method. The realization is actually relatively simple, first get the article release time, and then get the current time, compare the two times, and then determine the article release time display method, the code is as follows.

<?php if($days 
    

Directly copy the above code to the place you need to display on it, of course, we can also write the above function into a function, so that the call will not have to repeat so much code, the template will also look a little more concise.

Function to adjust the time display according to the freshness of the article

A partner asked how to write the above function as a function of the way, well, for your convenience, directly written as a function posted.

function wizhi_human_time(){

    $post_time = get_the_time('U');
    $now = current_time('timestamp');
    $diff = (int) abs( $post_time - $now );
    $days = round( $diff / DAY_IN_SECONDS );

    if($days < 1){
        echo '' ;
    }elseif($days == 2){
        echo '' ;
    }else{
        echo '';
    }

}

Paste the above function into the theme'sfunctions.phpin it, and then call it that way where needed:

 <?php wizhi_human_time(); ?>

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *