Giter VIP home page Giter VIP logo

Comments (1)

justintadlock avatar justintadlock commented on September 27, 2024

Actually, I went back and looked over that code. Here's a version that should be a bit better (but my math usage might need some double-checking):

public function readingTime( int $words_per_min = 200 ): string
{
        // Strip tags and get the word count from the content.
        $count = str_word_count( strip_tags( $this->content() ) );

        // Get the ceiling for minutes.
        $time_mins  = intval( ceil( $count / $words_per_min ) );
        $time_hours = 0;

        // If more than 60 mins, calculate hours and get leftover mins.
        if ( 60 <= $time_mins ) {
                $time_hours = intval( floor( $time_mins / 60 ) );
                $time_mins  = intval( $time_mins % 60 );
        }

        // Set up text for hours.
        $text_hours = sprintf(
                Str::nText( '%d Hour', '%d Hours', $time_hours ),
                number_format( $time_hours )
        );

        // Set up text for minutes.
        $text_mins = sprintf(
                Str::nText( '%d Minute', '%d Minutes', $time_mins ),
                number_format( $time_mins )
        );

        // If there are no hours, just return the minutes.
        // If there are no minutes, just return the hours.
        if ( 0 >= $time_hours ) {
                return $text_mins;
        } elseif ( 0 >= $time_mins ) {
                return $text_hours;
        }

        // Merge hours + minutes text.
        return sprintf( '%s, %s', $text_hours, $text_mins );
}

from post-reading-time-block.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.