使用的strtotime从DOM文档元素转换日期
-
20-09-2019 - |
题
我解析使用卷曲获取HTML文件之后,使用DOM文档的一些数据。代码看起来是这样
$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
}
我无法通过的strtotime转换$var
到的时间戳。我不知道为什么,我知道$cols->item(1)->nodeValue
回到我想要的价值,我甚至试过exploing和它的内爆到另一个变量,但我仍然无法使用的strtotime将其转换为一个时间戳。我还直接测试的值
strtotime('11 Jan 2010');
和它没有返回一个时间戳,所以,我应该怎么办?
解决方案
我无法通过重现该问题的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";
}
打印2010-01-12 00:00:00
不隶属于 StackOverflow