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.


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

Recommended Answers

All 5 Replies

You can use substr.

  $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
$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.

$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

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, learning, and sharing knowledge.