Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

leading zeroes in integers

Status
Not open for further replies.

MasterKaos

Programmer
Joined
Jan 17, 2004
Messages
107
Location
GB
I'm having trouble with MySQL queries failing when the month is one digit not two. As in 1 won't work but 01 will. Are there any php functions for formatting numbers or strings to include integers with leading zeroes? I've put togther a simple three line funciton that does it, but i wonder if there's a better way? It could get a bit tedious if there was more than two digits and other formatting like commas etc to worry about.

----------------------------------------------------------------------------
The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.
 
Are there any php functions for formatting numbers or strings to include integers with leading zeroes?

How are you getting your date in the first place?
PHP's date function for example, can return a date in many different formats.
including the numeric representation of a month, with or withut leading zeros.

Does this help you?
 
think you are looking for:
$today = date("d.m.Y") approximately.

try it and i think you will like it!
 
well the whole problem here is that i need the zeroes whacked onto something that starts off as being an integer, not a unix timestamp or any other kind of date value. Its a plain old integer and has to be becuase it's used to iterate thourgh loops and as an array index etc. I know i could convert it to a date then format it but thats more long-winded than what i'm doing now. I think i'll stick to this for now:
Code:
function format_month($month)
{
	settype($month, "string");
	if (strlen($month)==1)
		$month = "0" . $month;

	return $month;
}
Works fine i was just wonderring if it could be done automatically by PHP but maybe not.

----------------------------------------------------------------------------
The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.
 
well, I suppose if you read the manual you will discover the str_pad command:
$month = str_pad($month, 2, "0", STR_PAD_LEFT);

any better.

my answer - rtfm
 
You'd need a substring of str_pad to preserve a 2 digit field, otherwise you could end up with a month of 0012

$date=substr(str_pad($month,2,'0',STR_PAD_LEFT),-2);

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
you do not need a substr on a pad command as long as you set the max string length to the correct one! read the manual! the whole point of pad is that is makes a string up to the length given in the command:
$month = str_pad($month, 2, "0", STR_PAD_LEFT);
^max length of string!

do you not think I would test this before i posted?
and sleipnir214 solution would also work!
 
Ok thanks guys! i think sprintf() was the function i was looking for :-) but i guess something this simple can be done a number of ways.



----------------------------------------------------------------------------
The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.
 
I must admit I'm normally very good at reading the manual, a misunderstanding between it and myself some time ago has caused me to think that was tha maximum amount of padding.

Thanks for the pointer, it'll save me a few characters here n there :-)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top