首页 > 解决方案 > php date_create_from_format 无法正常工作

问题描述

当我使用

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

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

标签: phpdate

解决方案


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

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

推荐阅读