How can one format single-digit minutes or seconds as two digits when using DateInterval in PHP?

When using DateInterval in PHP, single-digit minutes or seconds may be displayed without a leading zero, which can affect the formatting of time values. To ensure that single-digit minutes or seconds are displayed as two digits, you can use the sprintf function with the '%02d' format specifier to pad the values with zeros if necessary.

$interval = new DateInterval('PT5M30S');
$formattedMinutes = sprintf('%02d', $interval->i);
$formattedSeconds = sprintf('%02d', $interval->s);

echo $formattedMinutes . ' minutes ' . $formattedSeconds . ' seconds'; // Output: 05 minutes 30 seconds