Powered by InterCreatives | Site Map
Search:
NewSourceMedia Popular Tutorials: PHP Javascripts Web Hosting
SoftwareTutorials
CATEGORIES
Job Listings
 
Home / PHP / Math

PHP Leading Zero for Double Digit Numbers


Question:
In php, how do I format my numbers so that all numbers have a double digit. I am displaying the number of days in a month and have to match the date formatted for day.

Answer:
Just check the number value during a loop in php to see if it's less then 10.
Php code is below:
If so then add the leading zero to your string.

<?
for ($num = 1; $num <= 31; $num++) {
if($num<10)
$day = "0$num"; // add the zero
else
$day = "$num"; // don't add the zero
echo "<p>$day</p>";
?>

Next PHP Page:
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  [28]  29  30  31  32 





Hosting by iPowerWeb.com - 50 Gb for $7.95