开发者

PHP: Best way to convert a datetime to just time string?

I have a datetime that I pulled from the database that looks like this: 2000-01-01 08:00开发者_运维技巧:00

Now I need to convert it so all I have left is: 08:00:00

What is the best way to do this?


substr('2000-01-01 08:00:00', 11);


You could use

substr($timestamp, -8);


you can also use the php strtotime function to turn it into a datetime object, then use the format method on it to get a pretty representation however you want. That way you can put in am/pm, do 24 hour or 12 hour, do whatever you want with the date, etc.

$date = strtotime('2000-01-01 08:00:00');
echo $date->format('H:i:s');


I should have thought of this earlier...easiest way would be to use the MySQL TIME() function to only select the time in the first place.


You can use date and enter the needed time format after parsing timestamp to str

date('h:i a', strtotime('2000-01-01 08:00:00'))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜