Solved

convert number into string using php

Posted on 2008-06-11
4
1,612 Views
Last Modified: 2008-06-12
Hi all.

I have a page where multiple records are shown. One of the pieces of data shown on each record is the time. This has been added to the database in 2 seperate fields timehr and timemin. the users add the time using 2 drop down list menus. the values of each hour and min do have the required 0 so i have 01,02,03, etc but when the data is added to the database 06 becomes 6, hence my question.

When i echo both timehr and timemin it works fine but when say its 6am in the databse the fields are filled with timehr = 6 and timemin = 0.

I want to add a zero to the left of timehr and to the right of timemin. so all times read 01:30, 06:00 etc and not 1:30, 6:0 etc

I have tried the following:
$s_Time = str_pad($info['timehr'], 2,'0', STR_PAD_LEFT) . ':' . str_pad($info['timemin'], 2, '0', STR_PAD_LEFT);

It doesnt work, instead i get 00:00 for every record regardless of what characters are in the data fields.

I am guessing it has somethiong to do with the numbers not being strings? Therefore it doesnt count any characters and thus gives 00.

Do i need to convert my $info['timehr'] into a string or am i complety barking up the wrong tree?


Can anyone help please?

Thanks in advance!
0
Comment
Question by:satmanuk
4 Comments
 
LVL 5

Expert Comment

by:pbeirne
ID: 21765823
If you are using mySQL you can set the timehr and timemin fields to type TINYINT, length 2 with an attribute of UNSIGNED ZEROFILL.  That way it will save all numbers with leading zeros.
0
 
LVL 9

Accepted Solution

by:
Rurne earned 500 total points
ID: 21765840
How are you populating $info?  Are you sure the values are being copied correctly from $_POST?
0
 
LVL 29

Expert Comment

by:rdivilbiss
ID: 21765893

<?PHP

// 02:05

$info['timehr']=2;

$info['timemin']=5;

$fulltime = substr('00'.$info['timehr'],strlen-2).':'.substr('00'.$info['timemin'],strlen-2);

echo $fulltime.'<br>';
 

// 12:30

$info['timehr']=12;

$info['timemin']=30;

$fulltime = substr('00'.$info['timehr'],strlen-2).':'.substr('00'.$info['timemin'],strlen-2);

echo $fulltime.'<br>';
 

// 22:00

$info['timehr']=20;

$info['timemin']=0;

$fulltime = substr('00'.$info['timehr'],strlen-2).':'.substr('00'.$info['timemin'],strlen-2);

echo $fulltime.'<br>';

?>

Open in new window

0
 
LVL 1

Author Comment

by:satmanuk
ID: 21767771
I had the follwoing lines:

//Puts it into an array
  while($info = mysql_fetch_array( $data ))
 {

Like a silly man i am i had my str_pad lines of code above the above lines of code.

So the code hadnt dealt with $info{'blah'] yet.

That will teach me for coding through the night......

@Rurne

You put me on the right trafck so you get the points.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now