Php strotime()的Bug

Php strotime()的Bug,php,web-scraping,screen-scraping,Php,Web Scraping,Screen Scraping,简单的HTMLDOM库用于从网页中提取时间戳strotime将提取的时间戳转换为MySQL时间戳 问题:在有效时间戳上使用strotime()时,返回NULL(请参见2:)。但是,如果在第二个示例中没有使用简单的HTMLDOM,那么一切都会正常工作 发生了什么,如何解决这个问题 输出: 1:2013-03-03, 12:06PM 2: 3:1970-01-01 00:00:00 var\u dump($time) PHP include_once(path('app') . 'librarie

简单的HTMLDOM库用于从网页中提取时间戳<然后使用code>strotime将提取的时间戳转换为MySQL时间戳

问题:在有效时间戳上使用
strotime()
时,返回
NULL
(请参见
2:
)。但是,如果在第二个示例中没有使用简单的HTMLDOM,那么一切都会正常工作

发生了什么,如何解决这个问题

输出:

1:2013-03-03, 12:06PM
2:
3:1970-01-01 00:00:00
var\u dump($time)

PHP

include_once(path('app') . 'libraries/simple_html_dom.php');

// Convert to HTML DOM object
$html = new simple_html_dom();
$html_raw = '<p class="postinginfo">Posted: <date>2013-03-03, 12:06PM EST</date></p>';
$html->load($html_raw);

// Extract timestamp
$time = $html->find('.postinginfo', 0);
$pattern = '/Posted: (.*?) (.).T/s';
$matches = '';
preg_match($pattern, $time, $matches);
$time = $matches[1];

echo '1:' . $time . '<br>';
echo '2:' . strtotime($time) . '<br>';
echo '3:' . date("Y-m-d H:i:s", strtotime($time));
var\u dump($time)


根据
var\u dump()
,从HTML代码中提取的
$time
字符串长度为25个字符

您看到的字符串,
“2013-03-03,12:06PM”
,只有19个字符长

那么,那6个额外的字符在哪里?很明显,你要解析的字符串实际上是
“2013-03-0312:06PM”
。但是,当您将其打印到HTML文档中时,浏览器会将该
解析为HTML标记


要查看它,请使用浏览器中的“查看源代码”功能。或者,更好的方法是,在打印任何不应该包含HTML代码的变量时,使用
htmlspecialchars()

你能添加简单的\u HTML\u dom.php代码吗?@MIIB-Sure:几乎没有可能是相关的,你100%确定你的输入是相同的吗?@WesleyMurch是的,我复制了时间戳
2013-03-03,美国东部时间12:06 pm
从第一个示例到第二个示例使用var_dump而不是echo检查隐藏字符
include_once(path('app') . 'libraries/simple_html_dom.php');

// Convert to HTML DOM object
$html = new simple_html_dom();
$html_raw = '<p class="postinginfo">Posted: <date>2013-03-03, 12:06PM EST</date></p>';
$html->load($html_raw);

// Extract timestamp
$time = $html->find('.postinginfo', 0);
$pattern = '/Posted: (.*?) (.).T/s';
$matches = '';
preg_match($pattern, $time, $matches);
$time = $matches[1];

echo '1:' . $time . '<br>';
echo '2:' . strtotime($time) . '<br>';
echo '3:' . date("Y-m-d H:i:s", strtotime($time));
// Extract posting timestamp
$time = 'Posted: 2013-03-03, 12:06PM EST';
$pattern = '/Posted: (.*?) (.).T/s';
$matches = '';
preg_match($pattern, $time, $matches);
$time = $matches[1];

echo '1:' . $time . '<br>';
echo '2:' . strtotime($time) . '<br>';
echo '3:' . date("Y-m-d H:i:s", strtotime($time));
1:2013-03-03, 12:06PM
2:1362312360
3:2013-03-03 12:06:00
string(19) "2013-03-03, 12:06PM"