PHP DateTime,解析字符串日期失败 [英] PHP DateTime, parsing string date fails
问题描述
这里他们得到解析如下:
$ startDate = $ request-> query-> get('start');
$ endDate = $ request-> query-> get('end');
$ logger-> info('startdate is:'$ startDate。',endDate是:'。$ endDate。'');
$ start = new \DateTime($ startDate);
$ end = new \DateTime($ endDate);
当我记录这两个参数时,可能是
startdate是:Wed Jan 12 2011 00:00:00 GMT 0100(CET)
startDate是:2013年1月12日星期一00:00:00 GMT 0100(CET)
到目前为止这么好,但是如果我记录DateTime的时间上面的字符串返回
DateTime对象([date] => 0100-01-12 00:00:00 [timezone_type] => 2 [timezone] => GMT)
DateTime对象([date] => 0100-01-15 00:00:00 [timezone_type] => 2 [timezone] = > GMT)
您可以看到,DateTime不代表相同的日期
我可以从这些字符串中创建一个有效的DateTime吗?
更新:
我尝试使用createFromFormat
like
$ startDate = $ request-> query-> get('start');
$ endDate = $ request-> query-> get('end');
$ start = new \DateTime :: createFromFormat('D M d Y h:i:s e + O(T)',$ startDate);
$ end = new \DateTime :: createFromFormat('D M d Y h:i:s e + O(T)',$ endDate);
但会导致异常:
FatalErrorException:解析:语法错误,意外的'createFromFormat'(T_STRING),
中的期望变量(T_VARIABLE)或'$'
我也尝试过:
$ start = new \DateTime(\ DateTime :: createFromFormat('DM d Y h:i:s e + O(T)',$ startDate));
$ end = new \DateTime(\DateTime :: createFromFormat('D M d Y h:i:s e + O(T)',$ endDate));
但是从现在开始创建一个新的日期(2014-01-21 12:28:57 )
我只是没有正确的。
有任何帮助,谢谢提前!
您的输入日期时间字符串 Wed Jan 12 2011 00:00:00 GMT 0100(CET) / code>在
DateTime()
或 strtotime()
中无效/标准。请参阅 date_parse()
函数来查看您的日期时间字符串的解析方式:
print_r(date_parse('Wed Jan 12 2011 00:00:00 GMT 0100(CET)'));
使用 DateTime :: createFromFormat()
根据具体格式返回DateTime对象的静态方法。
im passing a startDate end an endDate as a get request parameter to a method, here they get parsed like :
$startDate=$request->query->get('start');
$endDate=$request->query->get('end');
$logger->info('startdate is :'.$startDate.', endDate is : '.$endDate.'');
$start=new \DateTime($startDate);
$end=new \DateTime($endDate);
when i log those two parameters, they may be
startdate is: Wed Jan 12 2011 00:00:00 GMT 0100 (CET)
startDate is: Sat Jan 12 2013 00:00:00 GMT 0100 (CET)
so far so good, but if i log the DateTime´s instanciated from the string above it returns
DateTime Object ( [date] => 0100-01-12 00:00:00 [timezone_type] => 2 [timezone] => GMT )
DateTime Object ( [date] => 0100-01-15 00:00:00 [timezone_type] => 2 [timezone] => GMT )
you can see, the DateTime does not represent the same Date
can i make a valid DateTime from those Strings ?
Update :
i tryed to use createFromFormat
like
$startDate=$request->query->get('start');
$endDate=$request->query->get('end');
$start=new \DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$startDate);
$end=new \DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$endDate);
but that causes exception :
FatalErrorException: Parse: syntax error, unexpected 'createFromFormat' (T_STRING), expecting variable (T_VARIABLE) or '$' in
i also tryed :
$start=new \DateTime(\DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$startDate));
$end=new \DateTime(\DateTime::createFromFormat('D M d Y h:i:s e+O (T)',$endDate));
But that creates Dates a new Date from right now ( 2014-01-21 12:28:57 )
I just dont get it right.
for any help, thanks in advance!
Your input datetime string Wed Jan 12 2011 00:00:00 GMT 0100 (CET)
is not valid/standard for use in DateTime()
or strtotime()
. See date_parse()
function to see how your datetime string is being parsed:
print_r( date_parse('Wed Jan 12 2011 00:00:00 GMT 0100 (CET)') );
Use DateTime::createFromFormat()
static method to return DateTime object according to the specific format.
这篇关于PHP DateTime,解析字符串日期失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!