Hi, I have inserted time in Database using CURRENT_TIME() and Data type is Time. Now I want to show the Time in AM and PM format. Please guide me.
Thanks in advance....

Recommended Answers

All 11 Replies

is this a mysql database?
check this point in the manual:
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date-format
(other databases have similar commands)
I find it best to get the data right when selecting it, so use something like

SELECT DATE_FORMAT(your-date-column, '%r');

to retrieve the data so its ready to be displayed.

Using PHP is also possible of course:

echo date_format($datetime, 'g a') . "\n";

Michael

its my code but its print code which i used to show time in am pm

<?
   $con = mysql_connect("localhost","root","root");
   if(!$con)
     {
     die('Could not connect: ' . mysql_error());
    }
   mysql_select_db("onm", $con);


  $cmd=$_REQUEST["cmd"];
  $recid=$_REQUEST["recid"];
    $d=$_REQUEST["d"];
  
  if(isset($cmd)) {
     if($cmd=="Accept") {
       $sql="update gims_request set status='Accept' where recid='$recid'";
       mysql_query($sql);
     }
     if($cmd=="Reject") {
       $sql="update gims_request set status='Reject' where recid='$recid'";
       mysql_query($sql);
     }
  } 
$result = mysql_query("SELECT * FROM gims_request where status='N.A'");


while($row = mysql_fetch_array($result))
  {
$form=<<<POST
<form method="post" action="gims_request_show.php">
 Type Reason For Rejection(250words)<br>
  <textarea name="d" id="d" cols="30" rows="3" ></textarea>
   <input type="hidden" name="recid" value="$row[0]"/>
   <table cellpadding=2 cellspacing=2 width=970px style=font-size:12px>
      <tr bgcolor=#669933>
       <td bgcolor=#669933>Site ID</td><td>Vendor</td><td>Request Date</td><td>Item Code</td><td>Quantity</td><td>Unit</td><td>Collection Person</td><td>Remarks</td><td>TIME</td>
      </tr>
         <tr  bgcolor=#99cc33>
       <td> $row[0]  </td><td>$row[1]</td><td>$row[2]</td><td>$row[3]</td><td>$row[4]</td><td>$row[5]</td><td>$row[6]</td><td>$row[7]</td>[B]<td> . echo date_format($row[10], 'g a').;</td>[/B]
      </tr>
	  
	  
      <tr>
        <td colspan="3">
           <input type="submit" name="cmd" value="Accept"/>
           <input type="submit" name="cmd" value="Reject"/>
        </td>
      </tr>
   </table> 
</form>

POST;

echo $form;
  }
?>

Skipping the code that has no relevance to your problem:

$date = new DateTime(); // this will be a database request
echo date_format($date, 'g:i a');

responds with:

9:34 am

(I've added the minutes.)
Personally, i wouldn't use

$form=<<<POST

This makes your code less readable, and it serves no purpose. You can simply output the HTML code as you go, inserting the time as you go

while($row = mysql_fetch_array($result)) {
	$rowDate = date_format($date, 'g:i a');
	?>

	<form method="post" action="gims_request_show.php">

		...etc...
		<td><?=$rowDate?></td>
		...etc...

	</form>
<?php
}

$form<<<POST is neccessary for my page. because my page structure needs. please tell me any solution which shows time in am and pm with in the

$form<<<POST 
.....
POST;

echo $form;
$date = new DateTime(); // this will be a database request
$rowDate = date_format($date, 'g:i a');

$form=<<<POST
	$rowDate
POST;

echo $form;

outputs:

10:21 am

I want to format $row[10] and show in am and pm. and that is inside $form<<<POST
please guide me.

I want to format $row[10] and show in am and pm. and that is inside $form<<<POST
please guide me.

$form=<<<POST
	$row[10]
POST;
 
echo $form;
$form=<<<POST
	$row[10]
POST;
 
echo $form;

Hi, i have store time in Db and field name is time
Now I want to show that time which is stored in DB
I do not need to format Current Time.

and $row[10] is actually time store in Db.
I do not need to format current, I need to format $row[10] in AM and PM.

sorry, my mistake

//.. database stuff...

$rowDate = date_format($row[10], 'g:i a');
 
$form=<<<POST
	$rowDate
POST;
 
echo $form;

You can't use the function inside the
<<<POST
POST
only strings, so you have to prepare it beforehand.

sorry, my mistake

//.. database stuff...

$rowDate = date_format($row[10], 'g:i a');
 
$form=<<<POST
	$rowDate
POST;
 
echo $form;

You can't use the function inside the
<<<POST
POST
only strings, so you have to prepare it beforehand.

when I use this code it gives error

Warning: date_format() expects parameter 1 to be DateTime, string given in D:\xampp\htdocs\gims_request_show.php on line 116

when I use this code it gives error

Warning: date_format() expects parameter 1 to be DateTime, string given in D:\xampp\htdocs\gims_request_show.php on line 116

You have to cast the string value to a datetime:

$rowDate = date_format(new DateTime($row[10]), 'g:i a');
 
$form=<<<POST
	$rowDate
POST;
 
echo $form;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.