This question already has answers here:
ask about php summarize 01 + 01 = 02
(2 answers)
Closed 7 years ago.
How get the number with format is leading zero ex:01
and how to make format number like that my problem is from this query
$try= $this->login->get_id_child('00',3);
$count_try = count($try);
$id ="00";
$new = $id.$count_try;
echo $new;
the result from echo $new is 003
data in array $try=array(000,001,002) and try is count by $count_try the result is 3. but I want to make the result from $count_try is 03 so I can join that variable to my id so I can get $new=00003 but if the data in
$try=array(000,001,002,003,004,005,006,007,008,009);
so the value of $new is 0010
You can use str_pad -
echo str_pad($new, 2, "0", STR_PAD_LEFT);
str_pad()
Update
$count_try = 10;
$new = str_pad($count_try, 4, "0", STR_PAD_LEFT);
echo $new;
Try this :
$id ="0010";
$new = (int) $id + 1;
$new = str_pad($new, 4, "0", STR_PAD_LEFT);
echo $new; //0011
Related
This question already has answers here:
PHP integer part padding
(5 answers)
Closed 9 years ago.
hi I'm generating an random integers using mt_rand(1, 99999) and if its not equal to 5 digits it will add 0 on the front of the integers.
here's my first try:
$random_number = mt_rand(1, 99999);
$ran_len = strlen($random_number);
while ($ran_len != 5 ) {
$add_ran_num = str_pad($random_number, 1, "0", STR_PAD_LEFT);
}
echo $add_ran_num;
printf('%05d', mt_rand(1, 99999));
You don't need strlen nor while loop for that :
$random_number = mt_rand(1, 99999);
$add_ran_num = str_pad($random_number, 5, "0", STR_PAD_LEFT);
echo $add_ran_num;
This will do what you want
$random_number = str_pad(mt_rand(1, 99999), 5, "0", STR_PAD_LEFT);
You don't update the value of $ran_len:
$random_number = mt_rand(1, 99999);
$ran_len = strlen($random_number);
while ($ran_len != 5 ) {
$add_ran_num = str_pad($random_number, 1, "0", STR_PAD_LEFT);
$ran_len = strlen($random_number);//<---
}
echo $add_ran_num;
Thus the loop is infinite.
This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
<?php
$str = "02";
$int = 1;
$result = $str-$int;
echo $result // 1
?>
But I need result = 01
Don't tell me "0".$str-$int;
<?php
$str = "02";
$int = 1;
printf('%02d', $str-$int);
or
<?php
$str = "02";
$int = 1;
$result = sprintf('%02d', $str-$int);
// do something with $result here
see http://docs.php.net/manual/en/function.sprintf.php
try
printf("%02d",$str-$int);
The explanation of the numerous formatting possibilities of printf are explained in the sprintf docs
<?
$str = "02";
$int = 1;
echo sprintf("%02d", (int)$str - $int);
?>
Use str_pad() where you set it to pad the left with 0's until the length of string is 2 chars.
echo str_pad($str - $int, 2, '0', STR_PAD_LEFT);
This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
Is it possible in PHP to echo out a number with zeroes at the beginning?
For example
<?php
$i = 0001;
$i++;
echo $i;
?>
And when it print out, I want it to be like.
0002
Is it possible? Thanks :)
Yes, it is possible. You can do this using str_pad() method. Basically, this method adds a given value till a required length is achieved.
A Basic example of this would be:
echo str_pad($i, 4, "0", STR_PAD_LEFT);
Here,
4 represents the output length
"0" represents the string used to pad, till the length is achieved.
Demo Example:
<?php
$i = 0001;
$i++;
echo str_pad($i, 4, "0", STR_PAD_LEFT);
$i = 1;
$i++;
printf("%04d", $i); // 0002
printf - output a formatted string
%04d - echo a 4 digit number, pad with 0's
try this
echo str_pad($i, 4,"0",STR_PAD_LEFT);
read the documentation here : http://php.net/manual/en/function.str-pad.php
$input = 0001;
echo str_pad(++$input, 4, "0", STR_PAD_LEFT);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
simple regex in php, formatting decimal number
How to convert a digit in x format to x.xx format in php?
example :
I have a number 5. Need to convert to 5.00
$n = 5;
$n = number_format($n, 2, '.', '');
You should try number_format():
$your_number = 5;
echo number_format($your_number, 2); // displays 5.00
Manual
See http://php.net/manual/en/function.number-format.php
number_format('your digit', 2 or 1, '.', '');
$number = 125
$no = number_format($number, 2, '.', '');
output = 125.00
$x = 5;
$x = sprintf('%.2f', $x);
echo $x;
This question already has answers here:
Zero-pad digits in string
(5 answers)
Closed 2 years ago.
I have a variable which contains the value 1234567.
I would like it to contain exactly 8 digits, i.e. 01234567.
Is there a PHP function for that?
Use sprintf :
sprintf('%08d', 1234567);
Alternatively you can also use str_pad:
str_pad($value, 8, '0', STR_PAD_LEFT);
Given that the value is in $value:
To echo it:
printf("%08d", $value);
To get it:
$formatted_value = sprintf("%08d", $value);
That should do the trick
When I need 01 instead of 1, the following worked for me:
$number = 1;
$number = str_pad($number, 2, '0', STR_PAD_LEFT);
echo str_pad("1234567", 8, '0', STR_PAD_LEFT);
sprintf is what you need.
EDIT (somehow requested by the downvotes), from the page linked above, here's a sample "zero-padded integers":
<?php
$isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);
?>
Though I'm not really sure what you want to do you are probably looking for sprintf.
This would be:
$value = sprintf( '%08d', 1234567 );
Simple answer
$p = 1234567;
$p = sprintf("%08d",$p);
I'm not sure how to interpret the comment saying "It will never be more than 8 digits" and if it's referring to the input or the output. If it refers to the output you would have to have an additional substr() call to clip the string.
To clip the first 8 digits
$p = substr(sprintf('%08d', $p),0,8);
To clip the last 8 digits
$p = substr(sprintf('%08d', $p),-8,8);
If the input numbers have always 7 or 8 digits, you can also use
$str = ($input < 10000000) ? 0 . $input : $input;
I ran some tests and get that this would be up to double as fast as str_pad or sprintf.
If the input can have any length, then you could also use
$str = substr('00000000' . $input, -8);
This is not as fast as the other one, but should also be a little bit faster than str_pad and sprintf.
Btw: My test also said that sprintf is a little faster than str_pad. I made all tests with PHP 5.6.
Edit: Altough the substr version seems to be still very fast (PHP 7.2), it also is broken in case your input can be longer than the length you want to pad to. E.g. you want to pad to 3 digits and your input has 4 than substr('0000' . '1234', -3) = '234' will only result in the last 3 digits
$no_of_digit = 10;
$number = 123;
$length = strlen((string)$number);
for($i = $length;$i<$no_of_digit;$i++)
{
$number = '0'.$number;
}
echo $number; /////// result 0000000123
I wrote this simple function to produce this format: 01:00:03
Seconds are always shown (even if zero).
Minutes are shown if greater than zero or if hours or days are required.
Hours are shown if greater than zero or if days are required.
Days are shown if greater than zero.
function formatSeconds($secs) {
$result = '';
$seconds = intval($secs) % 60;
$minutes = (intval($secs) / 60) % 60;
$hours = (intval($secs) / 3600) % 24;
$days = intval(intval($secs) / (3600*24));
if ($days > 0) {
$result = str_pad($days, 2, '0', STR_PAD_LEFT) . ':';
}
if(($hours > 0) || ($result!="")) {
$result .= str_pad($hours, 2, '0', STR_PAD_LEFT) . ':';
}
if (($minutes > 0) || ($result!="")) {
$result .= str_pad($minutes, 2, '0', STR_PAD_LEFT) . ':';
}
//seconds aways shown
$result .= str_pad($seconds, 2, '0', STR_PAD_LEFT);
return $result;
} //funct
Examples:
echo formatSeconds(15); //15
echo formatSeconds(100); //01:40
echo formatSeconds(10800); //03:00:00 (mins shown even if zero)
echo formatSeconds(10000000); //115:17:46:40
You can always abuse type juggling:
function zpad(int $value, int $pad): string {
return substr(1, $value + 10 ** $pad);
}
This wont work as expected if either 10 ** pad > INT_MAX or value >= 10 * pad.