Estimating reading time is fine even when it is wrong
A reading-time estimate is a rough promise. It is useful because it tells the reader whether they are looking at four minutes or forty, and it is wrong for almost every individual reader.
The formula
Strip the markup, count the words, divide. The interesting decisions are what you strip and what you divide by.
$content = (string) get_post_field( 'post_content', $post_id );
$content = wp_strip_all_tags( strip_shortcodes( $content ) );
$words = preg_split( '/\s+/u', trim( $content ), -1, PREG_SPLIT_NO_EMPTY );
return max( 1, (int) ceil( count( $words ) / 200 ) );
Why 200
200 words per minute is the common anchor, and it is far too fast for technical writing. Code samples are read slower than prose, not faster, which is why this theme lands deliberately on the conservative side of the usual range.
The only real failure mode is pretending the number is precise. Round it, never show a decimal, and let the reader adjust.