无法在PHP中解析时间字符串

无法在PHP中解析时间字符串

问题描述:

I need to create a DateTime from the value received from a form. The problem is that the value is received like a string: "2016-10-10T08:29:06.959Z" and I need to received like 2016-10-10T08:29:06.959Z without quotes, because if I receive with quotes I've got the next error:

DateTime::__construct(): Failed to parse time string ("2016-10-14T22:00:00.000Z") at position 0 ("): Unexpected character

When I try to transform the value to a DateTime with:

$fechaHasta = new DateTime($params["fechaHasta"]);

If I try to use:

$fecha2 = DateTime::createFromFormat("d-m-Y H:i:s", $data["fechaHasta"]);

I've got an empty object in $fecha2.

The form from where I send the data is:

<form id = "formSearch" class = "menu-table" method = "post" action = "<?php echo $this->basePath('/privado/actividades-planificadas/pai/exportdatatoexcel'); ?>">
    <input type = "hidden" name = "codigoPPM" value = "{{searchForm.codigoPPM}}" />
    <input type = "hidden" name = "fechaDesde" value = {{searchForm.dt1}} />
    <input type = "hidden" name = "fechaHasta" value = {{searchForm.dt2}} />
    <input type = "hidden" name = "estado" value = "{{searchForm.estadoSelected.id}}" />
    <button type = "submit">
        Exportar datos a Excel&nbsp;&nbsp;&nbsp;<img title = "Exportar tabla a Excel" src = "/img/logo-excel.png" />
    </button>
</form> 

So, what I have to do to send the value without quotes or transform the value received in DateTime.

我需要根据从表单收到的值创建一个DateTime。 问题是收到的值就像一个字符串:“2016-10-10T08:29:06.959Z”我需要收到类似 2016-10-10T08:29:06.959Z code>的引号, 因为如果我收到引号,我会收到下一个错误: p>

DateTime :: __ construct():无法解析时间字符串(“2016-10-14T22: 00:00.000Z“)位置0(”):意外字符 p> blockquote>

当我尝试将值转换为DateTime时: p>

  $ fechaHasta = new DateTime($ params [“fechaHasta”]); 
  code>  pre> 
 
 

如果我尝试使用: p>

  $ fecha2 = DateTime :: createFromFormat(“dmY H:i:s”,$ data [“fechaHasta”]); 
  code>  pre> 
 
  

我在$ fecha2中有一个空对象。 p>

我发送数据的表单是: p>

 &lt  ; form id =“formSearch”class =“menu-table”method =“post”action =“&lt;?php echo $ this-&gt; basePath('/ privado / actividades-planificadas / pai / exportdatatoexcel');  ?&gt;“&gt; 
&lt; input type =”hidden“name =”codigoPPM“value =”{{searchForm.codigoPPM}}“/&gt; 
&lt; input type =”hidden“name =”fechaDesde“  value = {{searchForm.dt1}} /&gt; 
&lt; input type =“hidden”name =“fechaHasta”value = {{searchForm.dt2}} /&gt; 
&lt; input type =“hidden”name  =“estado”value =“{{searchForm.estadoSelected.id}}”/&gt; 
&lt; button type =“submit”&gt; 
 Exportar datos a Excel&amp; nbsp;&amp; nbsp;&amp; nbsp;&lt;  ; img title =“Exportar tabla a Excel”src =“/ img / logo-excel.png”/&gt; 
&lt; / button&gt; 
&lt; / form&gt; 
  code>  pre> 
  
 

那么,我必须做什么来发送没有引号的值或转换DateTime中收到的值。 p> div>

$date = '"2016-10-14T22:00:00.000Z"';
=> ""2016-10-14T22:00:00.000Z""

$fechaHasta = new DateTime($date);
Exception with message 'DateTime::__construct(): Failed to parse time string ("2016-10-14T22:00:00.000Z") at position 0 ("): Unexpected character'

$fechaHasta = new DateTime(trim($date, '"')); /* « notice the trim */
=> DateTime {#174
     +"date": "2016-10-14 22:00:00.000000",
     +"timezone_type": 2,
     +"timezone": "Z",
   }

So you just trim the " away:

$fechaHasta = new DateTime(trim($params["fechaHasta"], '"'));

This should work fine.

try strtotime function and you can set your required format as well.

example:

$date = date('Y-m-d H:i:s' , strtotime("2016-10-14T22:00:00.000Z"))

function trim() has option to remove specific characters from string: e.g.:

echo trim('example"','"');

will output: example