尝试在PHP中创建对象DateTime时出错 [英] Error when trying to create an object DateTime in PHP

查看:552
本文介绍了尝试在PHP中创建对象DateTime时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我有一个用PHP编写的项目。当我尝试在表数据集中显示日期时出现错误。这是生成错误的语句:

  $ data = new DateTime($ registro [previsao de entrega]; 

错误信息是这样的:


致命错误:未捕获的异常异常与消息
'DateTime :: __ construct()无法解析时间字符串(2 dez 2011
16:00)在位置0(2) :
中的意外字符C:\www\fluxo_producao\Telas\TelaFluxoProducao.php:941堆栈跟踪:#0
C:\www\fluxo_producao\Telas\TelaFluxoProducao .php(941):
DateTime - > __ construct('2 dez 2011 16:0 ...')#1 {main}抛出
C:\www\fluxo_producao\Telas\\我的发现是,如果我将参数手动更改为2006年12月12日,则可以通过以下方式进行操作:(1)在第941页上的\\ TelaFluxoProducao.php




<例如,函数有效,但变量所传递的日期是12 Dez 2006(巴西格式,顺便说一下,我是巴西^ _ ^),我发现服务器中的默认时区是America / Sao_Paulo...我必须更改功能或参数以使其转换格式指定?

解决方案

验证您已将服务器设置为正确的时间区域:

  setlocale(LC_TIME,'pt_BR'); 

这是 strftime() 和AFAIK所有的日期/时间相关函数和 DateTime class使用相同的库,这将需要/尊重此设置。


Today I got a project written in PHP. There's an error when I try to show a date in a table data set. This is the statement that generates the error:

$data = new DateTime($registro["previsao de entrega"];

The error message is this:

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() Failed to parse time string (2 dez 2011 16:00) at position 0 (2): Unexpected character' in C:\www\fluxo_producao\Telas\TelaFluxoProducao.php:941 Stack trace: #0 C:\www\fluxo_producao\Telas\TelaFluxoProducao.php(941): DateTime->__construct('2 dez 2011 16:0...') #1 {main} thrown in C:\www\fluxo_producao\Telas\TelaFluxoProducao.php on line 941

What I discovered by myself was that if I change the parameter manually to "12 Dec 2006" for example, the function works. But the date the variable is passing is "12 Dez 2006" (Brazilian format, by the way, I'm Brazilian ^_^), and i found too that the default timezone in the server is "America/Sao_Paulo"... What do I have to change in the function or parameters to make it convert the format specified?

解决方案

Verify that you have set your server to the correct time locale:

setlocale(LC_TIME, 'pt_BR');

This is required for strftime() and AFAIK all the date/time related functions and the DateTime class use the same library which will require/respect this setting.

这篇关于尝试在PHP中创建对象DateTime时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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