Solved

convert number into string using php

Posted on 2008-06-11
4
1,615 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

789 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