954,576 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Using datetime variable to just display time

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'];
						  
						 
						  }
?>
daniel7912
Newbie Poster
3 posts since Sep 2005
Reputation Points: 10
Solved Threads: 0
 


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.

lyrico
Junior Poster in Training
94 posts since Dec 2010
Reputation Points: 33
Solved Threads: 15
 
$gpstime=$row['gpstime'];
$ss= strtotime($gpstime);
print date("H:i:s", $ss);


Does it help?

TechySafi
Junior Poster in Training
99 posts since Oct 2010
Reputation Points: 10
Solved Threads: 14
 

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.

daniel7912
Newbie Poster
3 posts since Sep 2005
Reputation Points: 10
Solved Threads: 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

mschroeder
Work Harder
Team Colleague
666 posts since Jul 2008
Reputation Points: 279
Solved Threads: 131
 


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

lyrico
Junior Poster in Training
94 posts since Dec 2010
Reputation Points: 33
Solved Threads: 15
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: