Learn to Implement Estimated Reading Time using PHP Part 2: Final Implementation with Source Code
November 26, 2019

This is the Second and Last Part in Estimated Reading Time series. Take a look at the Part 1 if you don't know the basic functions. If you know them, skip Part 1.

Learn to Implement Estimated Reading Time using PHP Part 1: The Basics

Let's move forward form the part 1 now. Let's say we have we have our whole blog post in a variable named as $article. In most blog websites, that's what they do. They store the whole story in the database and then retrieve init to a variable. 

Take a look at the DEMO link provided at the end of this article and see what we have in the variable $article.

Now the whole blog post has many <p> tags which we don't want to count in our reading time as users won't see them. So we will remove them using the strip_tags function as discussed in the Part 1.


The code above will remove all HTML tags from out blog post. Now, we can go ahead and count the words. Let's do it.

$article = str_word_count(strip_tags($post));

In the code above I am counting the value after removing the tags and we can write down both of those function like that. Also, I am storing the word count in the same variable $article.

After that we will need to add an mathematical equation to count the time. Now, we have to assume the words per minute which is generally 200 to 250 per minute for an average person on most of the blogs. I am going to use 250 words per minute in our calculation.


Now if we use the $article/250 as it is and store in the variable then it would give us the float value but we want to round it up to the nearest decimal point so I have used round function.

That's it. We have a small counter now which will show the users time to read.


Now this part is not necessary as it's up to you how you want to display the time on the article. 

If you choose to display like "1 Min Read" or "2 Min Read", then there is no issue in printing the $EstTimeToRead variable directly using echo statement. 

But if you want to print the word "Minute" in it then you have to add a simple IF ELSE condition in your code as follows.

if($EstTimeToRead <=1){
  $EstTimeToRead = "1 Minute read";
  $EstTimeToRead = $EstTimeToRead . " Minutes Read";

After adding above few lines of code into your PHP script, you'll be able to print the word "Minute" when there is just 1 minute and "Minutes" when there are 2 or more minutes to read.


Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 How to uninstall Cocoapods from the Mac OS? #6 Solution to “TypeError: ‘x’ is not iterable” in Angular 9

Recently Posted

Feb 24 WordPress: How to loop through a repeater field in ACF? Feb 24 How to get ACF values from custom post type? Feb 24 WordPress get_posts Examples Feb 24 How to modify the latest post array using get_posts() in WordPress? Feb 22 WordPress: How to get ACF field values from another post? Feb 22 WordPress: How to print ACF repeater field values?

You might also like these

How to detect the Blog Page in WordPress?WordPressCan Firewall and IDPS Stop DDoS Attack?MiscHow to use GROUPING SETS to boost GROUP BY queries in Postgres?PostgresHow to create a CSS full page background image?CSSEXISTS and NOT EXISTS in PostgresPostgres3 Types of Arrays in PHPPHP