Solved

convert number into string using php

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 dynamically set the form action using jQuery.

863 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

23 Experts available now in Live!

Get 1:1 Help Now