Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 5th, 2004, 08:41 AM
Authorized User
 
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default Add padding zero to year

Hi there,

I wrote this simple code:

<select name="year">
<?php
$curYear = gmDate("y");
for ($i = $curYear; $i <= ($curYear + 10); $i++)
{
echo "<option value=\"$i\" $sel_year>$i</option>\n";
}
?>
</select>

But I'm running into a slight problem, the output prints the following list:
04
5
6
7
8
9
10
11
12
13
14

However, I need to have a padding zero in front of the years '05 through '09.. How do I make that happen?

Thanks!
 
Old August 6th, 2004, 07:00 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts
Default

This can be done with a printf type call, but a specialised (and much simpler) function called str_pad exists.

$paddedYear = str_pad($curYear, 2, "0", STR_PAD_LEFT);

Take it easy,
Dan
 
Old August 6th, 2004, 07:09 AM
Authorized User
 
Join Date: Jul 2004
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Daniel, that worked perfectly!!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Padding Zeros jmss66 Classic ASP Basics 5 January 28th, 2008 07:38 PM
IE7 Padding issue. socoolbrewster CSS Cascading Style Sheets 6 January 9th, 2007 04:56 AM
"Padding" a value in XSL francislang XSLT 1 September 3rd, 2004 07:56 AM
padding problem anshul HTML Code Clinic 5 July 24th, 2004 11:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.