gtotman Posted January 12, 2009 Share Posted January 12, 2009 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. Link to comment https://forums.phpfreaks.com/topic/140593-solved-problem-formatting-numbers-eg-2-as-02/ Share on other sites More sharing options...
jjacquay712 Posted January 12, 2009 Share Posted January 12, 2009 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? i would do the if statement, its the easiest way. Link to comment https://forums.phpfreaks.com/topic/140593-solved-problem-formatting-numbers-eg-2-as-02/#findComment-735740 Share on other sites More sharing options...
rickvandenham Posted January 13, 2009 Share Posted January 13, 2009 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. Link to comment https://forums.phpfreaks.com/topic/140593-solved-problem-formatting-numbers-eg-2-as-02/#findComment-735787 Share on other sites More sharing options...
gtotman Posted January 13, 2009 Author Share Posted January 13, 2009 Many thanks rickvandenham. That was exactly the type of simple solution we needed. I have tested it on our website and things work perfectly. gtotman Link to comment https://forums.phpfreaks.com/topic/140593-solved-problem-formatting-numbers-eg-2-as-02/#findComment-735803 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.