2015-11-10 34 views
0

使用mysql在php中选择查询以12小时格式显示时间和日期。使用php/Mysql中的select查询将日期/时间从24小时格式转换为12小时上午/下午

DB名称演示

表名学生

DB结构

Id  firstname lastname  pubdate 
int(11) varchar(100) varchar(100) timestamp 

我已经插入5个记录在学生的表并显示使用select查询一切都很正常 但我无法以适当的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']; 

} 

回答

1

迄今为止换算成12小时AM/PM使用

echo date('h:i:s a m/d/Y', strtotime($row['pubdate'])); 
4

使用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"; 
+0

谢谢@Rahautos。 – Sjay

+0

这是我的荣幸:) - @Sjay –

+0

如果我的答案可以帮助你,请接受我的回答@Sjay –

1

使用date()和PHP的strtotime()

echo date("Y-m-d h:i:s A", strtotime($row["pubdate"])); /* WILL ECHO 2015-11-10 02:50:24 PM */ 

参考here更多的日期格式。

+0

谢谢@Logan Wayne – Sjay

2

我认为你可以使用:

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) 
+0

谢谢@穆罕默德·阿尔斯兰 – Sjay

1

使用日期()和的strtotime()函数

echo date("Y-m-d h:i:s A", strtotime($row["pubdate"])); 
相关问题