简体   繁体   English

使用php / Mysql中的选择查询将日期/时间从24小时格式转换为12小时AM / PM

[英]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']));

Use date() and strtotime() of PHP: 使用PHP的date()strtotime()

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

Refer here for more date format. 请参阅此处以获取更多日期格式。

使用date()和strtotime()函数

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将日期/时间从24小时格式转换为12小时AM / PM? - How do I convert date/time from 24-hour format to 12-hour AM/PM? 如何确定当前语言环境在PHP中是否具有24小时或12小时时间格式? - How to determine if current locale has 24-hour or 12-hour time format in PHP? 如何在 PHP 中将时间从 AM/PM 转换为 24 小时格式? - How to convert the time from AM/PM to 24 hour format in PHP? 将日期时间与AM PM转换为24小时日期格式 - Convert date time with AM PM to 24 hour date format 将mySQL数据库的日志文件日期时间格式从12小时时钟批量转换为24小时时钟 - Bulk convert log file date time format from 12 hour clock to 24 hour clock for mySQL database 为什么用PHP无法将我的24小时转换为上午12点/下午12点? - Why can't I convert my 24 hour time to a 12 hour am/pm time with PHP? 如何将带有日期和时间AM / PM的字符串转换为24小时的mysql时间戳格式 - How to convert string with date and time AM/PM to 24 Hour mysql timestamp format 从PHP中没有AM和PM的12小时中减去24小时 - Deducing 24 hour time from a 12 hour time without AM and PM in PHP 在PHP中将24小时格式小时转换为12小时格式时间 - Convert 24 hour format hours into 12 hour format time in php MySQL SELECT按日期格式设置为24小时 - Mysql SELECT by date format as 24 hour
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM