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

放肆的年华 提交于 2020-01-06 04:43:05

问题


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?


回答1:


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



回答2:


You could use

substr($timestamp, -8);



回答3:


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');



回答4:


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.



来源:https://stackoverflow.com/questions/4349033/php-best-way-to-convert-a-datetime-to-just-time-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!