Modulus (%) operator return single zero. It is correct but i want to 2 zero after using modulus (%) operator. I don't want to use if condition.
$k = 60;
$result = $k%60;
echo "Result = ".$result;
Output
Result = 0
Expected output
Result = 00
Modulus (%) operator return single zero. It is correct but i want to 2 zero after using modulus (%) operator. I don't want to use if condition.
$k = 60;
$result = $k%60;
echo "Result = ".$result;
Output
Result = 0
Expected output
Result = 00
try it :
$k = 60;
$result = $k%60;
echo str_pad($result, 2, '0', STR_PAD_LEFT);
or test this method :
$result=0
$pad_length = 2;
$pad_char = 0;
$str_type = 'd'; // treats input as integer, and outputs as a (signed)
decimal number
$format = "%{$pad_char}{$pad_length}{$str_type}";
// output and echo
printf($format, $result);