MySQL time display help?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jim13090
Forum Newbie
Posts: 1
Joined: Mon Dec 06, 2010 7:27 pm

MySQL time display help?

Post by jim13090 »

I'm trying to echo a last login date and time from MySQL. Here's the code:

<p>You last logged in
<?php echo $row_Recordset1['MONTHNAME(last_login)'];?>
&nbsp;
<?php echo $row_Recordset1['DAYOFMONTH(last_login)'];?>
,
&nbsp;
<?php echo $row_Recordset1['YEAR(last_login)']; ?>
&nbsp;
<?php echo $row_Recordset1['HOUR(last_login)']; ?>
:
<?php echo $row_Recordset1['MINUTE(last_login)']; ?>
.
</p>

The code outputs the MySQL value of '2010-12-06 20:02:00' like this:

**You last logged in December 6, 2010 20:2.**

The problem I need to solve is that the MINUTE function returns a value in the range 1-59, and I need it to be 01-59. Also, I would prefer the hours to display in a 12-hour format instead of 24-hour (the output of HOUR function). I'm sure there's a simple solution to this, only I can't seem to figure it out.

Any assistance would be gratefully appreciated!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: MySQL time display help?

Post by Christopher »

One of several ways to do this is:

Code: Select all

<?php echo $sprintf('%02u', row_Recordset1['MINUTE(last_login)']); ?>
(#10850)
Post Reply