SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    Ireland
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Putting a zero infront of a number..

    Hey,

    I have a script that displays 1-31 through a loop.

    Code:
    <?php
    for ($i = 1; $i <= 31; $i++) {
      echo "<option value='$i'>$i</option>";
    }
    ?>
    Now, for 1-9, I want a zero to be infront of it. i.e. 01, 02..

    Is there an easy way to do this?

    - Matt

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    <?php
    for ($i = 1; $i <= 31; $i++) {
        echo '<option value="$i">'.sprintf('%02d',$i).'</option>';
    }
    Saul

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    If upi are padding it with zeros in order to store that date in a mysql "date" field - good news, you don't need to. Mysql is very forgiving, it takes 2008-1-1 and saves it as 2008-01-01.

    If you aren't then going on and saving the var into mysql then ignore this post, I only discovered that yesterday - anxious to share it with the world, see?

  4. #4
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also do str_pad
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  5. #5
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $i str_pad($i20STR_PAD_LEFT); 
    Will pad to a total length of 2 chars, using 0 as the pad character, adding characters to the left of the string.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •