0

Can someone show me how to do the following?
I have a variable named $day. If the value of $day is between 1 and 9 and it is represented by a single digit like “1”, I would like to set $day to 01.
How would I do this?

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by almostbob
1
<?php
$day = "2";
$newday = printf("%02s", $day); 
echo $newday;
?>

Result is 02.

Edited by Wraithmanilian: n/a

1
<?php
$day = "2";
$newday = printf("%02s", $day); 
echo $newday;
?>

Result is 02.

Thanks!
I also found this after a bit more searching:

if($day < 10) {
     $day = str_pad($day, 2, '0', STR_PAD_LEFT);
}
0

if statement is redundant
str_pad does nothing if the string is equal to or longer than the pad_length

$day = str_pad($day, 2, '0', STR_PAD_LEFT);

Edited by almostbob: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.