問題描述Nhận ngày định dạng trong php bằng cách sử dụng cơ sở dữ liệu mysql (Get formated date in php using mysql database) I want to get formated date in php from mysql database Like 21st March 2013 11:21AM
參考解法方法 1:You can use MySQL's built‑in function called DATE_FORMAT() SELECT DATE_FORMAT(NOW(), '%D %M %Y %h:%i%p')
- SQLFiddle Demo
- DATE_FORMAT()
OUTPUT ╔═════════════════════════╗
║ FORMATTED_DATE ║
╠═════════════════════════╣
║ 22nd March 2013 06:07AM ║
╚═════════════════════════╝
方法 2:You can use DATE_FORMAT MySQL function.
Example DATE_FORMAT(NOW(),'DATEFORMATE') // DATEFORMATE can be %b %d %Y %h:%i %p
OR STR_TO_DATE('$date', 'DATEFORMATE')
HOW TO USE IN QUERY SELECT STR_TO_DATE('YOUR DATE FIELD','DATEFORMAT') FROM table;
SELECT DATE_FORMAT(NOW(),'DATEFORMAT') FROM table;
DATEFORMATE Options
方法 3:Why not use MySQL's DATE_FORMAT? For your example, add this to your MySQL query: DATE_FORMAT(my_date, '%e %M %Y %h:%i %p')
方法 4:
Use DATE_FORMAT() SELECT * FROM table DATA_FORMAT(NOW(), '%D %M %Y %h:%i%p')
Tutorial on DATE_FORMAT 方法 5:What format is your database column in? If it's a UNIX timestamp, just feed it into date . If it's a date column (formatted as 'YYYY‑MM‑DD' or 'YYYY‑MM‑DD HH:MM:SS'), feed that into strtotime , then into date . So, assuming it's a date column, you'd want to do something like this:
$column_value = "2013‑03‑01 00:00:00"; // Get this from your database however you like
$formatted_date = date("jS F Y H:i A", strtotime($column_value));
The documentation on how to use date for formatting is in the PHP documentation. (by RishiKD、John Woo、Dipesh
Parmar、Jon、Sumit Bijvani、Telgin) 參考文件- Get formated date in php using mysql database (CC BY‑SA 3.0/4.0)
#SQL #MySQL
#PHP |