tags:

views:

39

answers:

4

The following is my code,

    $da =01;
    $cal_sunday = $da + 7;
    echo $cal_sunday;

the above calculation return the output as '8'.. i need the output as '08'... help..

+4  A: 

You can use sprintf to format your number:

echo sprintf('%02u', $cal_sunday);
Gumbo
+1  A: 

you can use printf eg printf("%02d",$cal_sunday);

ghostdog74
A: 

Have a look at the sprintf function

Your calculation produces and integer but you require a string.

Use sprintf to return the string in the format you need.

Jon Winstanley
A: 
$da = 1;
$cal_sunday = $da + 7;
printf('%02s', $cal_sunday);

printf() and sprintf() work similarly, except the first does a direct output, while the later returns the value so it can be assigned to another variable.

$da = 1;
$cal_sunday = sprintf('%02s', $da + 7);
echo $cal_sunday;
Dominic Barnes