如何从 PHP 中的日期时间获取 AM/PM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3404699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get AM/PM from a datetime in PHP
提问by Testadmin
I have a date time in a variable. My format is 08/04/2010 22:15:00
. I want to display this like 10.15 PM
. How to do this in PHP?
我在变量中有一个日期时间。我的格式是08/04/2010 22:15:00
. 我想像10.15 PM
. 如何在 PHP 中做到这一点?
回答by John Parker
回答by Mark Baker
$dateString = '08/04/2010 22:15:00';
$dateObject = new DateTime($dateString);
echo $dateObject->format('h:i A');
回答by Pekka
回答by phpmaster
<?php
$dateTime = new DateTime('now', new DateTimeZone('Asia/Kolkata'));
echo $dateTime->format("d/m/y H:i A");
?>
You can use this to display the date like this
您可以使用它来显示这样的日期
22/06/15 10:46 AM
回答by Sarfraz
回答by deck
for flexibility with different formats, use:
为了灵活使用不同的格式,请使用:
$dt = DateTime::createFromFormat('m/d/Y H:i:s', '08/04/2010 22:15:00');
echo $dt->format('g:i A')
Check the php manualfor additional format options.
检查php 手册以获取其他格式选项。
回答by rabin
PHP Code:
PHP代码:
date_default_timezone_set('Asia/Kolkata');
$currentDateTime=date('m/d/Y H:i:s');
$newDateTime = date('h:i A', strtotime($currentDateTime));
echo $newDateTime;
Output: 05:03 PM
输出: 05:03 PM
回答by imtaher
$currentDateTime = $row['date'];
echo $newDateTime = date('l jS \of F Y h:i:s A', strtotime($currentDateTime));
回答by Amit Ghosh Anto
Perfect answer for AM/PM live time solution
AM/PM 实时解决方案的完美答案
<?php echo date('h:i A', time())?>
回答by Andrei Stalbe
For (PHP >= 5.2.0):
对于(PHP >= 5.2.0):
You can use DateTime class. However you might need to change your date format. Didn't try yours.
The following date format will work for sure: YYYY-MM-DD HH-MM-SS
您可以使用 DateTime 类。但是,您可能需要更改日期格式。没试过你的。以下日期格式肯定会起作用:YYYY-MM-DD HH-MM-SS
$date = new DateTime("2010-04-08 22:15:00");
echo $date->format("g"). '.' .$date->format("i"). ' ' .$date->format("A");
//output
//10.15 PM
However, in my opinion, using .
as a separator for 10.15
is not recommended because your users might be confused either this is a decimal number or time format. The most common way is to use 10:15 PM
但是,在我看来,不建议使用.
for 作为分隔符,10.15
因为您的用户可能会混淆这是十进制数还是时间格式。最常见的方法是使用10:15 PM