Site logo

Archived topic

Estimated Reading Time in Minutes and Seconds

6 replies · Started by Melvin on March 17, 2022

Viewing posts 1–7 of 7

Hi,

I've followed the instructions here and added estimated reading time to my site successfully.

However, most of my posts are between 45 seconds to 1 minute 20 seconds. As a result, the Estimated Reading Time is always 1 minutes.

Firstly, 1 minutes (instead of the singular minute) is already grammatically incorrect. Secondly, if the result is always 1 minute, it's as good as not showing.

Is there a way to show the estimated reading time in minutes and seconds? Like 45 seconds, 1 minute 30 seconds, or 2 minutes 10 seconds?

Hi Melvin,

I see. If that’s the case, can you try this code instead and see if it works for you?:

function tu_estimated_reading_time() {
    $post = get_post();
    $content = $post->post_content;
    $wpm = 300; // How many words per minute.

    $clean_content = strip_shortcodes( $content );
    $clean_content = strip_tags( $clean_content );
    $word_count = str_word_count( $clean_content );
	$m = floor($word_count / 200);
    $s = floor($word_count % $wpm / ($wpm / 60));
	$time = $m . ' minute' . ($m == 1 ? '' : 's') . ', ' . $s . ' second' . ($s == 1 ? '' : 's');
    return $time;
}

add_filter( 'generate_post_date_output', function( $output ) {
    $output .= '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>';

    return $output;
} );

Kindly let us know how it goes. :)

Hey Fernando,

Thanks for the quick response. It works!

However, is there a way to change minutes to minute if it's 0?

Like "0 minute, 42 seconds" instead of "0 minutes, 42 seconds"?

Hey Fernando,

If it's too difficult, don't bother. I'll just use "m" and "s" instead. Like 0m:42s. No need to stress with singular and plural. :)

I see.

Can you try this instead?:

function tu_estimated_reading_time() {
    $post = get_post();
    $content = $post->post_content;
    $wpm = 300; // How many words per minute.

    $clean_content = strip_shortcodes( $content );
    $clean_content = strip_tags( $clean_content );
    $word_count = str_word_count( $clean_content );
	$m = floor($word_count / 200);
    $s = floor($word_count % $wpm / ($wpm / 60));
	$time = $m . ' minute' . ($m <= 1 ? '' : 's') . ', ' . $s . ' second' . ($s <= 1 ? '' : 's');
    return $time;
}

add_filter( 'generate_post_date_output', function( $output ) {
    $output .= '<div class="read-time">Reading time: ' . tu_estimated_reading_time() . '</div>';

    return $output;
} );

Kindly let us know how it goes. :)

Hey Fernando,

Yes, it worked perfectly!

Thank you so much for your help. I appreciate you.

Best regards,
Melvin Neo

You’re welcome Melvin! Glad that worked! Feel free to reach out anytime if you’ll need assistance with anything else. :)

This archived topic is closed to new replies.