开发者

Abbreviated string from MySQL 'time' field type in PHP

I have a MySQL field which stores times using the 'time' field type, such as '12开发者_StackOverflow:30:00' i.e. in the HH:MM:SS format.

In my PHP app I want to convert such HH:MM:SS data into a more readable string, such as '12.30pm'.

Is there any function for doing this, or will I have to do it by other means?

Many thanks!


I don't have a machine to test it on at the moment, but I would look at: strtotime and date. I believe that the following should be what you want:

$print_time = date("h.ia", strtotime($sql_time));

Where $print_time is the pretty printable time string and $sql_time is the SQL timestamp string.


If you don't mind changing you MySQL code you can just use MySQL's DATE_FORMAT() on the fetc:

SELECT DATE_FORMAT(table_name.time_col, '%l:%i %p') AS myFriendlyDate;


Why not use the datetime functions built into MySQL?

DATE_FORMAT will allow you to manipulated it however you want.

Or any of the other functions may help you with it ...

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_date-format

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜