Autolycus Autolycus - 7 months ago 24
PHP Question

parse string to time

I am looking to extract the time out of this string

Tue, 13 Mar 2012 04:48:34 -0400


I know that strtotime may work but I am not sure of implemation on how to just get
04:48:34
out from that string

thanks

Answer

You could use date_parse which returns an array with the date/time parts as separate elements. You can then extract the time parts, e.g.

$dateString = 'Tue, 13 Mar 2012 04:48:34 -0400';
$dateParts = date_parse( $dateString );
$timeString = $dateParts['hour'] . ':' . $dateParts['minute'] . ':' . $dateParts['second'];

Or better, if you have PHP5.3, you can use the DateTime class

$dateString = 'Tue, 13 Mar 2012 04:48:34 -0400';
$date = new DateTime( $dateString );
echo $date->format( 'H:i:s' );
Comments