Jump to content

[SOLVED] Changing the time field after importing data


ksduded

Recommended Posts

I am using a time field and importing data through a csv with the time as HH:MM (24 hour format with 11:00 pm as 23:00). It imports in MYSQL database as HH:MM:SS.

 

I want to convert it to 12 hour AM PM format. E.g. displaying 23:00 from the csv file to 11:00 PM.

Link to comment
Share on other sites

I tried this query

 

SELECT TIME_FORMAT('100:00:00', '%h %I');

 

but nothing happened. The data is under the field prog_time.

 

Do I need to enter that in the query as well?

 

plus i am looking to have a AM or PM function as well.. will I need to add %l in the end?

 

thanks in advance

Link to comment
Share on other sites

I tried this query

 

SELECT TIME_FORMAT('100:00:00', '%h %I');

 

but nothing happened. The data is under the field prog_time.

 

Do I need to enter that in the query as well?

 

plus i am looking to have a AM or PM function as well.. will I need to add %l in the end?

 

thanks in advance

I don't know what that means.

Link to comment
Share on other sites

I tried this query

 

SELECT TIME_FORMAT('100:00:00', '%h %I');

 

but nothing happened. The data is under the field prog_time.

 

Do I need to enter that in the query as well?

 

plus i am looking to have a AM or PM function as well.. will I need to add %l in the end?

 

thanks in advance

I don't know what that means.

 

ok i will try to explain again.

 

I am using PhpMyadmin to control the MYSQL features. I have a database with a table. It has a prog_time field which has the type 'time'.

If I enter type in an input as 23:00, it is stored as 23:00:00. I want the time format to store it as 11:00 PM.

 

I entered in SQL

 

SELECT TIME_FORMAT('100:00:00','%h %I)

 

but nothing happened.

 

so my question is, how do I change the format of the prog_time field to show an input of 23:00 to 11:00  PM

 

thanks

Link to comment
Share on other sites

If I enter type in an input as 23:00, it is stored as 23:00:00. I want the time format to store it as 11:00 PM.

 

so my question is, how do I change the format of the prog_time field to show an input of 23:00 to 11:00  PM

You DON'T change the format of the database field -- you format the field when you SELECT this column back.  Does that make sense?

Link to comment
Share on other sites

ok, so basically when I output it through php, i will use the different formatting types

Sure, but what I'm saying is that TIME_FORMAT() allows you get back the desired format from the database.

 

if i want to show a 11:00 PM time format by taking a data 23:00:00 stored in the prog_time field, what php code will i require?

 

I have been trying a lot of different functions, but can't work it out.

 

thanks in advance

Link to comment
Share on other sites

ok so i am using this query

 

SELECT TIME_FORMAT('prog_time', '%h %I %P');

 

and this is how i am using it in PHP

 

while($info = mysql_fetch_array($result))

{

if($info['prog_date'] == $selecteddate)

{

$programtime = $info['prog_time'];

$ptime = mysql_query("SELECT TIME_FORMAT('$programtime', '%h %i %P'");

echo .$ptime;

}

}

Link to comment
Share on other sites

You can't echo $ptime like that... it's a result set!  You need to (a) run mysql_fetch_assoc() and get back each row and (b) alias the expression to be able to reference it more easily.  In principle, you can cheat with mysql_result(), but it's bad practice.

 

Also, I thought this was coming from data stored in the DB.

 

Link to comment
Share on other sites

You can't echo $ptime like that... it's a result set! You need to (a) run mysql_fetch_assoc() and get back each row and (b) alias the expression to be able to reference it more easily. In principle, you can cheat with mysql_result(), but it's bad practice.

 

Also, I thought this was coming from data stored in the DB.

 

 

sorry for the noob questions, but I am trying to get hang of this.

 

The data is stored in a database. I have already extracted all the rows by using this query:

 

$result = mysql_query("SELECT * from tablehd");

 

 

I added a few lines to fetch the array (in bold), but I get the text Array after i run the code.

 

while($info = mysql_fetch_array($result)) 
{
if($info['prog_date'] == $selecteddate)
{ 
$programtime = $info['prog_time'];
$ptime = mysql_query("SELECT TIME_FORMAT('$programtime', '%h %i %P')");
$programtime1 = mysql_fetch_array($ptime);
echo $programtime1; 
}
}

Link to comment
Share on other sites

OK... now I understand... no need for 2nd query!

 

Change your initial query to:

 

$result = mysql_query("SELECT *, TIME_FORMAT( prog_time, '%h %i %P') AS prog_time2 from tablehd");

 

And then you can retrieve the value with "$info['prog_time2']" -- anywhere inside that while() loop.

 

I'm also not sure what that if() is doing....

 

Link to comment
Share on other sites

OK... now I understand... no need for 2nd query!

 

Change your initial query to:

 

$result = mysql_query("SELECT *, TIME_FORMAT( prog_time, '%h %i %P') AS prog_time2 from tablehd");

 

And then you can retrieve the value with "$info['prog_time2']" -- anywhere inside that while() loop.

 

I'm also not sure what that if() is doing....

 

 

great that nearly worked. Everything seems to be working fine, but it shows P at the end of the time rather than the AM or PM.

 

I am using MYSQL version 5.0.27

Link to comment
Share on other sites

OK... now I understand... no need for 2nd query!

 

Change your initial query to:

 

$result = mysql_query("SELECT *, TIME_FORMAT( prog_time, '%h %i %P') AS prog_time2 from tablehd");

 

And then you can retrieve the value with "$info['prog_time2']" -- anywhere inside that while() loop.

 

I'm also not sure what that if() is doing....

 

 

great that nearly worked. Everything seems to be working fine, but it shows P at the end of the time rather than the AM or PM.

 

I am using MYSQL version 5.0.27

 

ok i just changed a capital 'P' to a small 'p' and it worked.

;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.