lundi 26 septembre 2016

Wordpress: show different timestamp based on time

What's the best way to show a timestamp based on different times?

If a post is published under 24 hours display "Posted at {time}", else if a post is over 24 hours display "Posted on {date}", else if a post is modified display "Updated {modified_date}"

Any suggestions? I've done this so far:

function ci_posted() {
    $time_string = '<time itemprop="datePublished" content="%2$s" class="entry-date published updated" datetime="%1$s">%2$s</time>';
if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
    $time_string = '<time itemprop="datePublished" content="%2$s" class="entry-date published" datetime="%1$s">%2$s</time><time itemprop="dateModified" content="%4$s" class="updated" datetime="%3$s">%4$s</time>';
}

$time_string = sprintf( $time_string,
    esc_attr( get_the_date( 'c' ) ),
    esc_html( get_the_date() ),
    esc_attr( get_the_modified_date( 'c' ) ),
    esc_html( get_the_modified_date() )
); 

 $time_posted = esc_html( get_the_time() );
 $u_time = get_the_time('U'); 
 $u_modified_time = get_the_modified_time('U');

$posted_on = sprintf(
    _x( '%s', 'post date', 'ci' ),
    '' . $time_string . ''
);

if ($u_modified_time >= $u_time + 86400){ 
echo '<span class="posted-on"> Updated ' . $posted_on . ' at ' . $time_posted . '</span>';
} else {
 echo '<span class="posted-on"> Posted at ' . $time_posted . '</span>';
}

Aucun commentaire:

Enregistrer un commentaire