midesweb/reading-time

Reading time PHP class calculator

Installs: 2

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/midesweb/reading-time

v0.3.2 2025-07-14 17:19 UTC

This package is auto-updated.

Last update: 2025-12-14 18:11:15 UTC


README

EstimatedReadingTime is a lightweight PHP utility that calculates the estimated reading time (in minutes) for a given block of HTML or plain text content.

It uses a simple heuristic: an average reading speed of 120 words per minute. It strips HTML tags from the content before counting the words and rounds the result to the nearest full minute (rounding up if the leftover seconds exceed 30 seconds).

โœ… Features

  • Automatically strips HTML tags before processing
  • Calculates reading time based on word count
  • Easy to update the content dynamically
  • Supports human-readable output in English (EN) and Spanish (ES)

๐Ÿ”– Installation

composer require midesweb/reading-time

๐Ÿงฑ Basic Example

use Midesweb\ReadingTime\EstimatedReadingTime;

$content = "<p>This is a sample article with some <strong>HTML</strong> content.</p>";
$estimator = new EstimatedReadingTime($content);

$minutes = $estimator->getEstimatedReadingMinutes();

echo "Estimated reading time: {$minutes} minute(s)";

๐Ÿ” Updating the content

$estimator->updateContent("New content goes here...");
$minutes = $estimator->getEstimatedReadingMinutes();

๐ŸŒ Localized Read Time Output

You can get a localized string describing the estimated read time using getReadTime().
The default language is English (EN), but you can switch to Spanish (ES) using setLanguage():

$estimator = new EstimatedReadingTime($content);

// English (default)
echo $estimator->getReadTime(); 
// Output: "1 minute", "3 minutes", "1 hour and 5 minutes", etc.

// Spanish
echo $estimator->setLanguage('es')->getReadTime();
// Output: "1 minuto", "3 minutos", "1 hora y 5 minutos", etc.

๐Ÿ”’ Only EN and ES are supported. Invalid languages will throw an InvalidArgumentException.

๐Ÿงช How it works

  1. Strips HTML tags from the input.
  2. Counts words using str_word_count().
  3. Divides by 120 (average words per minute).
  4. Rounds up the result if more than 30 seconds remain.
  5. Optionally returns a localized human-readable string.

๐Ÿ’ก Requirements

  • PHP 7.4 or higher