Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Estimated reading time in PHP, by Brian Cray
In Drupal 7:
<?php
$postContent = render($content);
$word = str_word_count(strip_tags($postContent));
$m = floor($word / 200);
$s = floor($word % 200 / (200 / 60));
$est = $m . ' minute' . ($m == 1 ? '' : 's') . ', ' . $s . ' second' . ($s == 1 ? '' : 's');
?>
<p>Estimated reading time: <?php echo $est; ?></p>
Brian Cray's original, for Wordpress:
<?php
$mycontent = $post->post_content; // wordpress users only
$word = str_word_count(strip_tags($mycontent));
$m = floor($word / 200);
$s = floor($word % 200 / (200 / 60));
$est = $m . ' minute' . ($m == 1 ? '' : 's') . ', ' . $s . ' second' . ($s == 1 ? '' : 's');
?>
<p>Estimated reading time: <?php echo $est; ?></p>
Total props to Brian Cray: http://briancray.com/posts/estimated-reading-time-web-design/
@stemword

This comment has been minimized.

Copy link

stemword commented Aug 1, 2017

Why you divide by 200 ?

@gnysek

This comment has been minimized.

Copy link

gnysek commented Aug 5, 2017

@stemword According to web, average reading speed is between 230-280 words per minute, so he using 200 here is keeping some limit to provide pessimistic reading time. Most of people should read it faster.

@inetbiz

This comment has been minimized.

Copy link

inetbiz commented Mar 19, 2019

@mynameispj have you test this with WP Bakery shortcodes inclusion in post content?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.