Skip to content

Instantly share code, notes, and snippets.

@lidocaine
Created March 2, 2018 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lidocaine/ea70d0f67957c5300377b52096e680c7 to your computer and use it in GitHub Desktop.
Save lidocaine/ea70d0f67957c5300377b52096e680c7 to your computer and use it in GitHub Desktop.
A PHP snippet for displaying a DateInterval object ($diff) as a human readable string in WordPress.
$readable_diff = [];
if ( $diff->y ) {
$readable_diff[] = sprintf( _n( '%s year', '%s years', $diff->y, 'textdomain' ), $diff->y );
}
if ( $diff->m ) {
$readable_diff[] = sprintf( _n( '%s month', '%s months', $diff->m, 'textdomain' ), $diff->m );
}
if ( $diff->d ) {
$readable_diff[] = sprintf( _n( '%s day', '%s days', $diff->d, 'textdomain' ), $diff->d );
}
if ( $diff->h ) {
$readable_diff[] = sprintf( _n( '%s hour', '%s hours', $diff->h, 'textdomain' ), $diff->h );
}
if ( $diff->i ) {
$readable_diff[] = sprintf( _n( '%s minute', '%s minutes', $diff->i, 'textdomain' ), $diff->i );
}
if ( $diff->s ) {
$readable_diff[] = sprintf( _n( '%s second', '%s seconds', $diff->s, 'textdomain' ), $diff->s );
}
$readable_str = implode( ', ', $readable_diff );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment