new DateTime('2016-04-01 00:00:00')返回'2016-04-01 12:00:00

时间:2016-04-05 17:22:01

标签: php datetime

这可能是一个明智的选择,但我看不到解决方案。我有以下代码:

$begin = new DateTime('2016-03-14 00:00:00');
echo $begin->format('Y-m-d h:i:s');

输出结果为:

2016-03-14 12:00:00

我试图改变时区,设置时区,让它关闭。输出永远不会改变。是什么赋予了?我希望输出显示为“2016-03-14 00:00:00”。

4 个答案:

答案 0 :(得分:4)

您需要使用H原样24小时,h是12小时。 12:00:00是12:00或00:00:00:

$begin = new DateTime('2016-03-14 00:00:00');
echo $begin->format('Y-m-d H:i:s');

答案 1 :(得分:3)

应该是

$begin = new DateTime('2016-03-14 00:00:00');
echo $begin->format('Y-m-d H:i:s');
  

h一小时的12小时格式,前导零到01

     

H小时格式的一小时,前导零到00

http://php.net/manual/en/function.date.php

答案 2 :(得分:3)

h用于12小时制(上午/下午),因此您的模式缺少aA来完成:

$begin = new DateTime('2016-03-14 00:00:00');
echo $begin->format('Y-m-d H:i:s a');

会给你2016-04-01 12:00:00 am

如果您需要完整的24小时制,则需要使用H代替:

$begin = new DateTime('2016-03-14 00:00:00');
echo $begin->format('Y-m-d H:i:s');

会给出预期的2016-04-01 00:00:00

Here is the documentation for supported placeholders您可以使用format()

答案 3 :(得分:1)

如上所述,h用于12小时时钟格式,H用于24小时时钟格式。通过更改此信函,您的日期将按您的要求格式化。

$begin = new DateTime('2016-03-14 00:00:00');
echo $begin->format('Y-m-d H:i:s');

这是一个可用于PHP中的DateTime对象的格式列表(Source

Day of Month
____________
d   | Numeric, with leading zeros   01–31
j   | Numeric, without leading zeros    1–31
S   | The English suffix for the day of the month   st, nd or th in the 1st, 2nd or 15th.

Weekday
_______
l   | Full name  (lowercase 'L')    Sunday – Saturday
D   | Three letter name Mon – Sun

Month
______
m   | Numeric, with leading zeros   01–12
n   | Numeric, without leading zeros    1–12
F   | Textual full  January – December
M   | Textual three letters Jan - Dec

Year
____
Y   | Numeric, 4 digits Eg., 1999, 2003
y   | Numeric, 2 digits Eg., 99, 03

Time
____
a   | Lowercase am, pm
A   | Uppercase AM, PM
g   | Hour, 12-hour, without leading zeros  1–12
h   | Hour, 12-hour, with leading zeros 01–12
G   | Hour, 24-hour, without leading zeros  0-23
H   | Hour, 24-hour, with leading zeros 00-23
i   | Minutes, with leading zeros   00-59
s   | Seconds, with leading zeros   00-59
T   | Timezone abbreviation Eg., EST, MDT ...
Full Date/Time
c   | ISO 8601  2004-02-12T15:19:21+00:00
r   | RFC 2822  Thu, 21 Dec 2000 16:01:07 +0200