Hi,

At the moment I am pulling a record from the database which is in datetime format. Could someone please tell me how I can modify this to just display the time and exclude the date?

Many thanks for any help.

<?php

						$time = mysql_query("SELECT * FROM gps WHERE imei = '447779799009' ORDER BY gpstime DESC LIMIT 1");
						
											
				
					while($row = mysql_fetch_array($time))
						  {
						  
						  echo $row['gpstime'];
						  
						 
						  }
?>

You can use substr.
Example.

<?php
  
  $date="March 21, 2011 - 06:30 PM";
 
  $time = substr($date,17,8); // this means you will eliminate 17 characters (March 21, 2011 - ) "including the space", and then print out the 8 characters.
  echo $time; // the output will be 06:30 PM

 
?>

you can also google substr tags.

commented: thanks! +0
Member Avatar for TechySafi
$gpstime=$row['gpstime'];
$ss= strtotime($gpstime);
print date("H:i:s", $ss);

Does it help?

commented: thanks! +0

thanks to both of you :)

I ended up trying TechySafy's first and it worked but I will probably try out Lyrico's method too at some point.

PHP's DateTime (http://www.php.net/manual/en/book.datetime.php) object accepts the MySQL DateTime format natively.

<?php
$dt = '2038-12-31 23:59:59';
$dtObj = new DateTime( $dt );
echo $dtObj->format('h:i:s a').PHP_EOL;

$ss = strtotime($dt);
echo date("h:i:s", $ss).PHP_EOL;
11:59:59 pm
00:00:00

Keep in mind PHP's strtotime and date functions are not compatible with dates beyond Jan 19, 2038. (http://en.wikipedia.org/wiki/Year_2038_problem) e.g. The end date of a 30 year mortgage from today etc.

This has been resolved in the DateTime class since PHP 5.2

If your problem was solved, your can now marked this thread as solved. ^^,

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.