Skip to content

Commit

Permalink
Tweak the interval output again.
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbillion committed Jan 9, 2025
1 parent c9f4840 commit 665533a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
24 changes: 14 additions & 10 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2252,23 +2252,26 @@ function time_since( $older_date, $newer_date ) {
* Converts a period of time in seconds into a human-readable format representing the interval.
*
* Intervals less than an hour are displayed in minutes, and intervals less than a minute are
* displayed in seconds. All other intervals are displayed in the two largest units.
* displayed in seconds. All intervals are displayed in the two largest units.
*
* The `$accurate` parameter can be used to display an interval of less than an hour in minutes and seconds.
*
* Example:
*
* echo \Crontrol\interval( 40 );
* // 40 seconds
* echo \Crontrol\interval( 450 );
* // 7 minutes
* echo \Crontrol\interval( 3800 );
* // 1 hour 3 minutes
* echo \Crontrol\interval( 450, true );
* // 7 minutes 30 seconds
* echo \Crontrol\interval( 5678 );
* // 1 hour 34 minutes
*
* @param int|float $since A period of time in seconds.
* @param int|float $since A period of time in seconds.
* @param bool $accurate Whether to display the interval in minutes and seconds.
* @return string An interval represented as a string.
*/
function interval( $since ) {
$since = intval( $since );

function interval( $since, bool $accurate = false ) {
// Array of time period chunks.
$chunks = array(
/* translators: %s: The number of years in an interval of time. */
Expand All @@ -2291,11 +2294,12 @@ function interval( $since ) {
return __( 'now', 'wp-crontrol' );
}

if ( ( $since >= MINUTE_IN_SECONDS ) && ( $since < HOUR_IN_SECONDS ) ) {
if ( ( ! $accurate ) && ( $since >= MINUTE_IN_SECONDS ) && ( $since < HOUR_IN_SECONDS ) ) {
$num = intval( floor( $since / MINUTE_IN_SECONDS ) );
return sprintf(
/* translators: %s: The number of minutes in an interval of time. */
_n( '%s minute', '%s minutes', $since, 'wp-crontrol' ),
floor( $since / MINUTE_IN_SECONDS )
_n( '%s minute', '%s minutes', $num, 'wp-crontrol' ),
$num
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/schedule-list-table.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ protected function column_crontrol_interval( array $schedule ) {
$interval = sprintf(
'%s (%s)',
esc_html( "{$schedule['interval']}" ),
esc_html( interval( $schedule['interval'] ) )
esc_html( interval( $schedule['interval'], true ) )
);

if ( $schedule['is_too_frequent'] ) {
Expand Down

0 comments on commit 665533a

Please sign in to comment.