Current Article:

How to calculate estimate reading time using PHP?

How to calculate estimate reading time using PHP?

Average user’s reading speed is between 230-280 words per minute. We’ll use 200 here to keep some limit to provide pessimistic reading time.

Code:

function calculate_read_time($text) {
  $word = str_word_count(strip_tags($text));
  $m = floor($word / 200);
  $s = floor($word % 200 / (200 / 60));
  return $m . ' minute' . ($m == 1 ? '' : 's') . ', ' . $s . ' second' . ($s == 1 ? '' : 's');
}

Example:

echo calculate_read_time($some_log_text);

Output:

3 minutes, 31 seconds