PHP格式日期2个字符


php format date with 2 characters

我有一个这样的日期:

3/6/2014 7:20

我希望它的格式为

03/06/2014 07:20

最好的做法是什么?

就像:)

date("m/d/Y h:i");

参考手册查看这些值代表什么。我把这些拿过来。

m = Numeric representation of a month, with leading zeros 
d = Day of the month, 2 digits with leading zeros
Y = A full numeric representation of a year, 4 digits
h = 12-hour format of an hour with leading zeros
i = Minutes with leading zeros

你的格式有点模棱两可,虽然我假设因为AM/PM不在那里,时间是24小时格式。也不确定到货日期是日/月还是月/日。

$date = new DateTime("3/6/2014 7:20");
echo $date->format('m/d/Y H:i');

也不确定到货日期是日/月还是月/日。但你可以用createFromFormat

来处理
$date = new DateTime::createFromFormat("n/j/Y G:i", "3/6/2014 7:20");
echo $date->format('m/d/Y H:i');

阅读在线文档中的日期格式。还可以查看扩展作者的DateTime书以了解更多信息。

使用createFromFormat()函数按照指定的格式读取,并输出到需要的格式

http://www.php.net/manual/en/datetime.createfromformat.php

DateTime::createFromFormat -- date_create_from_format —
Returns new DateTime object formatted according to the specified format

的例子:

$date = DateTime::createFromFormat('j/n/Y', '3/6/2014 G:i');
echo $date->format('m/d/Y h:i');