PHP DateTime :: createFromFormat不起作用 [英] PHP DateTime::createFromFormat not working

查看:41
本文介绍了PHP DateTime :: createFromFormat不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DateTime :: createFromFormat 似乎无法正常工作,有人知道原因和/或解决方法吗?

DateTime::createFromFormat doesn't appear to be working correctly, does anyone know a reason and/or how to fix it?

给出以下代码:

var_dump(DateTime::createFromFormat('m', '02')->format('m'));
var_dump(DateTime::createFromFormat('n', '2')->format('n'));

我的预期输出将是

02
2

但是我实际上得到了:

03
3

示例: http://codepad.viper-7.com/e4hns6

我已经在许多服务器上对此进行了测试,包括:

I've tested this on a multitude of servers including:

  • Win PHP 5.4
  • Linux PHP 5.3
  • 毒蛇键盘

推荐答案

来自手册:

"如果格式不包含字符!,则部分未指定格式的生成时间将设置为当前系统时间."

"If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time."

以下应能按预期工作:

var_dump(DateTime::createFromFormat('!m', '02')->format('m'));
var_dump(DateTime::createFromFormat('!n', '2')->format('n'));

# result:
string(2) "02"
string(1) "2"

这篇关于PHP DateTime :: createFromFormat不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆