新的DateTime返回空的DateTime实例 [英] new DateTime returns empty DateTime instance

查看:108
本文介绍了新的DateTime返回空的DateTime实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

跟踪错误的来源,我发现这段代码在开发服务器和实时服务器中的行为有所不同:

Tracking the source of a bug, I've found that this piece of code behaves differently in the development and live servers:

<?php

ini_set('log_errors', FALSE);
ini_set('display_errors', TRUE);
error_reporting(E_ALL ^ E_STRICT);

var_dump(date_default_timezone_set('Europe/Madrid'));

try{
    $dt = new DateTime('2010-12-01');
    var_dump($dt);
}catch(Exception $e){
    var_dump($e);
}

在我的开发箱(PHP / 5.3.0)中,我得到了预期的输出:

In my dev box (PHP/5.3.0) I get the expected output:

bool(true)
object(DateTime)#1 (3) {
  ["date"]=>
  string(19) "2010-12-01 00:00:00"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/Madrid"
}

服务器(PHP / 5.2.14)我得到一个空白对象:

In the live server (PHP/5.2.14) I get a blank object:

bool(true)
object(DateTime)#1 (0) {
}

我已经丢弃了通常的犯罪嫌疑人(未找到默认时区,不可解析的日期字符串...)。此代码在两台服务器中的输出相同:

I've already discarded the usual suspects (missing default time zone, non-parseable date string...). This code has the same output in both servers:

<?php

ini_set('log_errors', FALSE);
ini_set('display_errors', TRUE);
error_reporting(E_ALL ^ E_STRICT);

var_dump(date_default_timezone_set('Europe/Madrid'));

$ts = strtotime('2010-12-01');
var_dump($ts, date('r', $ts));

打印:

bool(true)
int(1291158000)
string(31) "Wed, 01 Dec 2010 00:00:00 +0100"

我遗漏了一些明显的东西吗?

Am I missing something really obvious?

推荐答案

$date = new DateTime('2010-12-01');
echo $date->format('Y-m-d'); 

显然,您无法在5.2版本的DateTime上执行var_dump。可能与他们在5.2中设置类的方式有关

Apparently you cant do a var_dump on the 5.2 version of DateTime. Probably had to do with how they had the class setup in 5.2

这篇关于新的DateTime返回空的DateTime实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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