2013-01-08 26 views

回答

5
<?php 
$str = "02"; 
$int = 1; 

printf('%02d', $str-$int); 

<?php 
$str = "02"; 
$int = 1; 

$result = sprintf('%02d', $str-$int); 
// do something with $result here 

看到http://docs.php.net/manual/en/function.sprintf.php

+0

如果我想使變量? – l2aelba

+1

@ l2aelba您可以使用'sprintf()'代替。 +1這是更好的解決方案。 –

+0

l2aelba:那麼你會使用sprintf如http://stackoverflow.com/a/14216162/4833 – VolkerK

1

使用str_pad()在那裏你將它設置爲墊左側用0到字符串的長度爲2個字符。

echo str_pad($str - $int, 2, '0', STR_PAD_LEFT); 
+0

那就是!!!謝謝 – l2aelba

+2

@ l2aelba,我會用VolkerK的答案,因爲這是一個更好的方法。 –

4

嘗試

printf("%02d",$str-$int); 

的printf函數的格式衆多可能性的解釋的解釋

+0

對不起,我編輯了我的問題,+1 – l2aelba

3
<? 

$str = "02"; 
$int = 1; 

echo sprintf("%02d", (int)$str - $int); 

?>