在php中使用mysql-select查询以12小时格式显示时间和日期。
数据库名称演示
表名学生
数据库结构
Id firstname lastname pubdate
int(11) varchar(100) varchar(100) timestamp
我在学生表中插入了5条记录,并使用选择查询显示一切都很好但是我无法以正确的12小时格式显示pubdate!
有人能帮我进去吗成功了,谢谢!
代码
$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'];
}
使用MySQL DATE_FORMAT
DATE_FORMAT(pubdate,'%Y-%m-%d %h:%i %p')
你这样查询:-
$sql = "select id,firstname,lastname,
DATE_FORMAT(pubdate,'%Y-%m-%d %h:%i %p') as pubdate from student";
我认为您可以使用:
echo date('Y-m-d h:i:s', strtotime($row['pubdate']));
活样品
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']));
使用PHP:的date()
和strtotime()
echo date("Y-m-d h:i:s A", strtotime($row["pubdate"])); /* WILL ECHO 2015-11-10 02:50:24 PM */
请参阅此处了解更多日期格式。
使用date()和strtotime()函数
echo date("Y-m-d h:i:s A", strtotime($row["pubdate"]));