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.
Related
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
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.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am creating an incrementing number starting with 1001. If the number goes 1001,1002,1003... when it reaches 10, will it be formatted like 1010 or will it be 10010? I need it to just go in order and be 1010 and when it reaches 100, 1100.
$prefix = "1"; // update the prefix here
$number = 1;
$number++;
$unique = str_pad($number, 3, "0", STR_PAD_LEFT);
$unique = $prefix . $unique;
print_r($unique);
When your count reaches 10, the number printed will be 1010. As described here, str_pad "Pads a string to a certain length with another string" You can create a test with the following:
$prefix = "1"; // update the prefix here
$number = 1;
for ($number = 1; $number <= 100; $number++)
{
$unique = str_pad($number, 3, "0", STR_PAD_LEFT);
$unique = $prefix . $unique;
print($unique."\n");
}
When your count reaches 100, the number printed will be 1100.
However, if you were to go up to 1000, 11000 would be printed - str_pad apparently will not truncate the string to match the specified size.
It will be 1010, but you can test this yourself easily:
$prefix = "1"; // update the prefix here
$number = 9;
$number++;
$unique = str_pad($number, 3, "0", STR_PAD_LEFT);
$unique = $prefix . $unique;
print_r($unique); // 1010
The second argument of str_pad specifies padding. If padding is 3, then 1 becomes 001, 10 becomes 010, 100 becomes 100.
With your code it will be 10010.
It looks like you are making this more complicated than it needs to be. Why not just start with $number = 1001 and increment it and then turn it into a string?
$number = 1001;
$number++;
$unique = strval($number);
print_r($unique);
This question already has answers here:
How to pad single-digit numbers with a leading 0
(7 answers)
Closed 11 months ago.
I'm doing a for loop to count from 0001 up to 0999
How can i do this with php. All i've got so far is a normal php for loop.
Something like this?
for($i = 1; $i<=999; $i++){
echo str_pad($i, 4, '0', STR_PAD_LEFT);
}
Additionally, you can make use of sprintf() instead of str_pad(), but I think str_pad() looks much clearer than sprintf() in this case.
What you want to do is a normal loop and format the output:
for( $i=1; $i<=999; $i++) {
$myformat = str_pad($i, 4, '0', STR_PAD_LEFT);
// do something with $myformat
}
Try that code:
for($n=1;$n<=999;$n++)
{
$formatted_n = str_pad($n, 4, '0', STR_PAD_LEFT);
// add some code here
}
Documentation for str_pad
<?php
for($i=1; $i<=999; $i++) {
echo str_pad($i, 4, "0", STR_PAD_LEFT);
}
?>
Here is a version using sprintf():
foreach (range(1, 999) as $i){
echo sprintf("%04d", $i);
} // output: 000100020003...
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.