Jump to content

[SOLVED] Problem formatting numbers. eg. 2 as 02


gtotman

Recommended Posts

Hello,

 

Our web host recently upgraded from php 4.1.0 to 4.3.9 and some of our code is now displaying a different result.  Our database stores times in seconds and we convert the displayed time to minutes:seconds using php.  With the old php version, the following code would output 2:09.45 but now it displays 2:9.45 instead. 

 

<?php 
list($minutes, $seconds) = secs2mins(129.45);
printf ("%3d:%02.2f", $minutes, $seconds);

function secs2mins($secs)
{
$minutes = floor($secs / 60);
$seconds = $secs - ($minutes * 60);
return array ($minutes, $seconds);
}
?>

 

Is there a simple fix or do we need to do an if statement that will add a zero when $seconds is less than 10?

 

Thank you.

In the code %02.2f, the 02 means pad with the character 0 and only pad up to two characters.  This worked in your older version of php, but now you need to code this as %05.2f as the padding now refers to the length of the field rather than just the number of digits before the decimal point.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.