php date_create_from_format无法正常工作

时间:2018-08-31 10:54:51

标签: php date

当我使用

<?php
$mnth=date_format(date_create_from_format('m', '2'), 'F');
echo $mnth;
?>

它显示三月而不是二月。我在4月,6月,9月和11月有类似的问题。任何人对此都有想法。请帮助我

2 个答案:

答案 0 :(得分:3)

使用'!m'作为格式。 '!'将所有字段(年,月,日,小时,分钟,秒,分数和时区信息)重置为Unix Epoch。没有'!'所有字段都将设置为当前日期和时间。今天是31-08-2018,2月,4月,6月,9月和11月没有31天。

<?php
$mnth = date_format(date_create_from_format('!m', '2'), 'F');
echo $mnth;
?>

答案 1 :(得分:0)

如果我们不提供createFromFormat的日期,则默认为今天。 '!'重置所有字段(年,月,日,小时,分钟,秒,分数和时区信息)

$mnth = date_format(date_create_from_format('!m', '2'), 'F');