开发者

Convert date using strtotime from DOMDocument element

开发者 https://www.devze.com 2022-12-16 23:51 出处:网络
I\'m parsing some data using DOMDocument after fetching HTML file using curl. The codes look like this

I'm parsing some data using DOMDocument after fetching HTML file using curl. 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. 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. 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

$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

0

精彩评论

暂无评论...
验证码 换一张
取 消