简体   繁体   English

使用来自DOMDocument元素的strtotime转换日期

[英]Convert date using strtotime from DOMDocument element

I'm parsing some data using DOMDocument after fetching HTML file using curl. 使用curl获取HTML文件后,我正在使用DOMDocument解析一些数据。 The codes look like this 代码看起来像这样


$dom = new DOMDocument();
@$dom->loadHTML($content);
$tables = $dom->getElementsByTagName('table');
$rows = $tables->item($tblNum)->getElementsByTagName('tr');

foreach ($rows as $row) {
    $cols = $row->getElementsByTagName('td');
    $var = $cols->item(1)->nodeValue; // The problem is here
}

I can't convert $var to a timestamp using strtotime. 我无法使用strtotime将$var转换$var时间戳。 I don't know why, I know $cols->item(1)->nodeValue returned the value I want, I even tried exploing and imploding it into another variable, but I still can't convert it to a timestamp using strtotime. 我不知道为什么,我知道$cols->item(1)->nodeValue返回了我想要的值,我什至尝试将其分解并内插到另一个变量中,但是我仍然无法使用strtotime将其转换为时间戳。 I've also tested the value directly 我也直接测试了价值

strtotime('11 Jan 2010');

and it did return a timestamp, so, what should I do ? 它确实返回了时间戳,那么,我该怎么办?

I can't reproduce the problem using php 5.3.1/winxp 我无法使用php 5.3.1 / winxp重现该问题

$content = '<html><head><title>...</title></head><body>
  <table>
    <tr><td>X</td><td>12 Jan 2010</td></tr>
  </table>
</body></html>';
$tblNum = 0;

$dom = new DOMDocument();
@$dom->loadHTML($content);
$tables = $dom->getElementsByTagName('table');
$rows = $tables->item($tblNum)->getElementsByTagName('tr');

foreach ($rows as $row) {
  $cols = $row->getElementsByTagName('td');
  $var = $cols->item(1)->nodeValue;
  echo date('Y-m-d H:i:s', strtotime($var)), "\n";
}

prints 2010-01-12 00:00:00 打印2010-01-12 00:00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM