我爱占星网 我爱占星网
首页
编程
java
php
前端
首页 编程 java php 前端

DateTime返回错误的年份

My code is like following:

<?php
$newDate = new DateTime('2012');
echo $newDate->format('Y');

Why $newDate->format('Y') returns 2015 (current year) and not 2012?

Because 2012 is not a valid date string. By default the date and time set into the object will be the current, which is 2015. You can hint what format you are going to use with DateTime::createFromFormat

$date = DateTime::createFromFormat('Y', '2012');
echo $date->format('Y');

近期文章

  • 内容过滤 - 但允许嵌入内容的iframe
  • Mysql插入并选择查询一起转换为mysqli
  • 获得符合特定范围标准的可能组合(大小为N)
  • Wordpress自定义查询和粘贴帖子
  • 从ReflectionParameter中提取类型提示
  • PHP缩略图生成器不处理缩略图
  • 使用codeigniter发送电子邮件时出错
  • 用javascript / jquery替换选择输入
  • 变量后的变量锚标签Wordpress
  • 在php和Codeigniter中安排类别和子类别
  • str_replace正在替换我的字符串中的所有部分。
  • Symfony 2预填充表单,包含数据库中的数据
  • Symfony 2 - 在所有视图中包含表单并由一个控制器处理
  • AngularJS和PHP Undefined属性:stdClass
  • 使用codeigniter通过电子邮件验证注册
  • 用PHP读取的文件内容
  • 如何使用php和mysql按列用户名检查最后一行ref?
  • 对数组的一部分执行某些操作,然后将其重新插入到数组中?
  • 在Silex框架中生成实体
  • PHP中没有Curl的HTTP身份验证

Copyright ©2022 我爱占星 All Rights Reserved.

浙ICP备2022030071号-1

部分图文来自网络,如有侵犯您的版权,请告诉我们删除

友情链接:代码精华