在 PHP 中用前导零设置数字的格式

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?

转载于:https://stackoverflow.com/questions/1699958/formatting-a-number-with-leading-zeros-in-php

Use sprintf :

sprintf('%08d', 1234567);

Alternatively you can also use str_pad:

str_pad($value, 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);
?>

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

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);

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

This would be:

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

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.

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

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

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

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 

This works perfectly:

$number = 13246;
echo sprintf( '%08d', $number );
function duration($time){

    $hours = '';
    $minutes = '00:';
    $seconds = '00';

    if($time >= 3600){
        //0 - ~ hours
        $hours    = floor($time / 3600);
        $hours    = str_pad($hours, 2, '0', STR_PAD_LEFT) . ':';
        $time     = $time % 3600;
    }

    if($time >= 60){
        //0 - 60 minute     
        $minutes  = floor($time / 60);
        $minutes  = str_pad($minutes, 2, '0', STR_PAD_LEFT) . ':';
        $time     = $time % 60; 
    }

    if($time){
        //0 - 60 second
        $seconds  = str_pad($time, 2, '0', STR_PAD_LEFT);       
    }

    return $hours . $minutes . $seconds;

}

echo duration(59); // 00:59
echo duration(590); // 09:50
echo duration(5900); // 01:38:20