[英]convert date/time from 24-hour format to 12-hour AM/PM using select query in php/Mysql
Displaying Time and date in 12hour format using mysql select query in php. 使用mysql select php以12hour格式显示时间和日期。
DB Name Demo 数据库名称演示
Table name student 表名学生
DB Structure 数据库结构
Id firstname lastname pubdate
int(11) varchar(100) varchar(100) timestamp
I have inserted 5 records in student table and displayed using select query everything works great but i am unable to display pubdate in proper 12 hour format! 我已在学生表中插入5条记录,并使用select查询显示,一切正常,但我无法以正确的12小时格式显示发布日期!
please can somebody help me out in acheiving it Thanks! 请有人帮我解决这个问题谢谢!
Code 码
$sql = "select id,firstname,lastname,pubdate from student";
$results = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($results))
{
echo $row['id'];
echo $row['firstname'];
echo $row['lastname'];
echo $row['pubdate'];
}
use MySQL DATE_FORMAT 使用MySQL DATE_FORMAT
DATE_FORMAT(pubdate,'%Y-%m-%d %h:%i %p')
You query like that :- 您这样查询:
$sql = "select id,firstname,lastname,
DATE_FORMAT(pubdate,'%Y-%m-%d %h:%i %p') as pubdate from student";
i think you can use : 我认为您可以使用:
echo date('Y-m-d h:i:s', strtotime($row['pubdate']));
Live Sample 现场样本
h is used for 12 digit time
i stands for minutes
s seconds
a will return am or pm (use in uppercase for AM PM)
m is used for months with digits
d is used for days in digit
Y uppercase is used for 4 digit year (use it lowercase for two digit)
要将日期转换为上午12点,请使用
echo date('h:i:s a m/d/Y', strtotime($row['pubdate']));
使用date()和strtotime()函数
echo date("Y-m-d h:i:s A", strtotime($row["pubdate"]));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.