Jump to content

[SOLVED] Format TIME field from MySQL as 12-hour


mikera

Recommended Posts

I have a TIME field in MySQL that is stored in 24-our format that I want to display as 12-hour format on my site.  Is there a simple way to format the time correctly?  I've seen plenty of examples of how to format a DATETIME field, but nothing for just a TIME field.

 

Thanks,

Mike

Link to comment
Share on other sites

OK, so I'm fairly new to php so I need a little extra help to get this to work.  Here is the code I have currently that just pulls the value from the table.  How do I use your code in conjunction with this?

 

<?php echo $match->gametime;?>

 

Earlier on the page I have the $match table variable defined and gametime is the field name.  I'm just not sure where to put the code and how to echo it.

Link to comment
Share on other sites

Unfortunately I can't locate it.  This page is part of a Joomla component so there are a lot of external references that I don't quite understand yet.

 

Is there no way to just format a TIME value that is passed from a table like there seems to be for DATETIME?  I was hoping there was just a simple syntax like:

 

formatTIME(fieldname, %r)

 

...where %r is the 12-hour format variable.  That way I could end up with something basic like:

 

<?php echo formatTIME($match->gametime, %r);?>

 

My ignorance is really on display now!

 

Mike

Link to comment
Share on other sites

I tried and it gave me a long integer "1185084000" regardless of the time value in the field.

 

Maybe it would help if I told you I was storing the time as "09:30:00" or "17:45:00" in the field "$matches->gametime".  What I want to display in those examples would be "9:30 am" and "5:45 pm".  Leading zeros would be OK, too: "09:30 am" and "05:45 pm".

 

Thanks,

Mike

Link to comment
Share on other sites

try strtotime()

 

$dateTimestamp = strtotime($match->gametime);

 

$dateFormat = date($dateTimestamp, "h:i:s");

 

echo $dateFormat;

Right idea, but your date parameters were backwards.

 

echo date("h:i a", strtotime($match->gametime));

Link to comment
Share on other sites

Almost there, Hypnos.  But that code just gives me 12:00 am for every value.  Remember, my field is only a TIME field, not a DATETIME field, so it seems all the date() functions are ignoring the time value as it is stored as "hh:mm:ss".  Is there a similar function for TIME fields in MySQL?

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.