在 PHP 中使用前导零格式化数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1699958/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:33:51  来源:igfitidea点击:

Formatting a number with leading zeros in PHP

phpnumbersformat

提问by Andromeda

I have a variable which contains the value 1234567.

我有一个包含值的变量1234567

I would like it to contain exactly 8 digits, i.e. 01234567.

我希望它恰好包含 8 位数字,即01234567.

Is there a PHP function for that?

是否有一个 PHP 函数?

回答by reko_t

Use sprintf:

使用sprintf

sprintf('%08d', 1234567);

Alternatively you can also use str_pad:

或者,您也可以使用str_pad

str_pad($value, 8, '0', STR_PAD_LEFT);

回答by Kaze no Koe

Given that the value is in $value:

鉴于该值以 $value 为单位:

  • To echo it:

    printf("%08d", $value);

  • To get it:

    $formatted_value = sprintf("%08d", $value);

  • 呼应它:

    printf("%08d", $value);

  • 为拿到它,为实现它:

    $formatted_value = sprintf("%08d", $value);

That should do the trick

这应该够了吧

回答by Md. Shafiqur Rahman

When I need 01 instead of 1, the following worked for me:

当我需要 01 而不是 1 时,以下对我有用:

$number = 1;
$number = str_pad($number, 2, '0', STR_PAD_LEFT);

回答by joan16v

echo str_pad("1234567", 8, '0', STR_PAD_LEFT);

回答by Huppie

Though I'm not really sure what you want to do you are probably looking for sprintf.

尽管我不确定您想做什么,但您可能正在寻找sprintf

This would be:

这将是:

$value = sprintf( '%08d', 1234567 );

回答by Huppie

sprintfis what you need.

sprintf是你所需要的。

EDIT (somehow requested by the downvotes), from the page linked above, here's a sample "zero-padded integers":

编辑(downvotes 以某种方式要求),从上面链接的页面,这里有一个示例“零填充整数”:

<?php
    $isodate = sprintf("%04d-%02d-%02d", $year, $month, $day);
?>

回答by Peter Lindqvist

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.

我不确定如何解释“它永远不会超过 8 位数字”的评论,以及它是指输入还是输出。如果它指的是输出,则必须进行额外的 substr() 调用来剪辑字符串。

To clip the first 8 digits

剪辑前 8 位数字

$p = substr(sprintf('%08d', $p),0,8);

To clip the last 8 digits

剪辑最后 8 位数字

$p = substr(sprintf('%08d', $p),-8,8);

回答by AbcAeffchen

If the input numbers have always 7 or 8 digits, you can also use

如果输入的数字总是 7 或 8 位,您也可以使用

$str = ($input < 10000000) ? 0 . $input : $input;

I ran some tests and get that this would be up to double as fast as str_pador sprintf.
If the input can have any length, then you could also use

我进行了一些测试,发现这将是str_pador 的两倍sprintf
如果输入可以有任何长度,那么你也可以使用

$str = substr('00000000' . $input, -8);

This is not as fast as the other one, but should also be a little bit faster than str_padand sprintf.

这不如另一个快,但也应该比str_pad和快一点sprintf

Btw: My test also said that sprintfis a little faster than str_pad. I made all tests with PHP 5.6.

顺便说一句:我的测试也说sprintfstr_pad. 我使用 PHP 5.6 进行了所有测试。

Edit:Altough the substrversion 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

编辑:虽然该substr版本似乎仍然非常快(PHP 7.2),但如果您的输入可能比您想要填充的长度长,它也会被破坏。例如,您想填充到 3 位数字,而您的输入有 4 位,而substr('0000' . '1234', -3) = '234'只会导致最后 3 位数字

回答by Enigma Plus

I wrote this simple function to produce this format: 01:00:03

我写了这个简单的函数来生成这种格式: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 

回答by Rutvik kakadiya

$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