0

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'];
						  
						 
						  }
?>
4
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by lyrico
1

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.

Votes + Comments
thanks!
1
$gpstime=$row['gpstime'];
$ss= strtotime($gpstime);
print date("H:i:s", $ss);

Does it help?

Votes + Comments
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.

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.