Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract the time from a timestamp 1

Status
Not open for further replies.

kzn

MIS
Jan 28, 2005
209
0
0
GB
Hi, what is the best way to extract a time from a time stamp?

$date = "2004-12-12 12:30:00"

Should I use substr? or is there a better way or is there no right or wrong way?

Many thanks
 
Hi

To use [tt]substr()[/tt] you either have to be sure the month and day will be always 0 padded or use [tt]strpos()[/tt] to find out where the separator space is.

I would prefer to use [tt]explode()[/tt] :
Code:
Interactive shell

php > $date = "2004-12-12 12:30:00";

php > echo array_pop(explode(' ', $date));
12:30:00


Feherke.
[link feherke.github.com/][/url]
 
or you can use date/time methods built in to php

Code:
function getTimeFromDateStamp( $datestamp ){
 return @date('H:i:s', strtotime($datastamp));
}
//the @ is to suppress the warning you might get about not setting the default time zone.  ideally you will have done this in php.ini or in prior code.

Feherke's method of string manipulation will be less processor intensive.
 
Thank you both for your help, I am going to use Jpadie's method. Many thanks again to both of you for your help :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top