2017-10-06 70 views
0

这里是我的问题,我试着抓住日期和更改日期格式从2表中的特殊字符。它成功了。 但1表中的日期返回值为0000-00-00 00:00:00,另一个表成功。下面是代码,表格和输出。另一个数据日期值为空时,当抓取html

表1

<TABLE class="tab1" border="1" cellpadding="0" cellspacing="0" 
summary=""> 
<TR> 
<TH align=left colspan=2 bgcolor=#0066CC><H1> &nbsp;Start RIP Job</H1> 
</TH> 
</TR> 
<TR> 
<TH align=left> &nbsp; &nbsp;Send Date: 
</TH> 
<TD class="td1" align=left> &nbsp; &nbsp;1/9/2017 1:15 PM&nbsp; &nbsp; 
</TD> 
</TR> 
<TR> 
<TH align=left> &nbsp; &nbsp;RIP Start Date and Time: 
</TH> 
<TD class="td1" align=left> &nbsp; &nbsp;13:21:22 09/01/2017&nbsp; &nbsp; 
</TD> 
</TR> 
<TR> 
<TH align=left> &nbsp; &nbsp;RIP End Date and Time: 
</TH> 
<TD class="td1" align=left> &nbsp; &nbsp;13:21:33 09/01/2017&nbsp; &nbsp; 
</TD> 
</TR> 
<TR> 
<TH align=left> &nbsp; &nbsp;RIP Duration: 
</TH> 
<TD class="td1" align=left> &nbsp; &nbsp;11 seconds&nbsp; &nbsp; 
</TD> 
</TR> 
<TR> 
<TH align=left colspan=2 bgcolor=#0066CC><H1> &nbsp;End RIP Job</H1> 
</TH> 
</TR> 
</TABLE> 

表2

<TABLE class="tab1" border="1" cellpadding="0" cellspacing="0" 
summary=""> 
<TR> 
<TH align=left colspan=2 bgcolor=#0066CC><H1> &nbsp;Start RIP Job</H1> 
</TH> 
</TR> 
<TR> 
<TH align=left> &nbsp; &nbsp;Printer: 
</TH> 
<TD class="td1" align=left> &nbsp; &nbsp;RunJiang Flora 3204P&nbsp; 
&nbsp; 
</TD> 
</TR> 
<TR> 
<TH align=left> &nbsp; &nbsp;Send Date: 
</TH> 
<TD class="td1" align=left> &nbsp; &nbsp;9/29/2017 10:09 PM&nbsp; &nbsp; 
</TD> 
</TR> 
<TR> 
<TH align=left> &nbsp; &nbsp;RIP Start Date and Time: 
</TH> 
<TD class="td1" align=left> &nbsp; &nbsp;22:09:49 29/09/2017&nbsp; &nbsp; 
</TD> 
</TR> 
<TR> 
<TH align=left> &nbsp; &nbsp;RIP End Date and Time: 
</TH> 
<TD class="td1" align=left> &nbsp; &nbsp;22:10:13 29/09/2017&nbsp; &nbsp; 
</TD> 
</TR> 
<TR> 
<TH align=left> &nbsp; &nbsp;RIP Duration: 
</TH> 
<TD class="td1" align=left> &nbsp; &nbsp;24 seconds&nbsp; &nbsp; 
</TD> 
</TR> 
<TR> 
<TH align=left colspan=2 bgcolor=#0066CC><H1> &nbsp;End RIP Job</H1> 
</TH> 
</TR> 
</TABLE> 

CODE:

$source=file_get_contents("C://xampp/htdocs/Champion/machine-logs/LogPrinting04/nulldate.HTML"); 
$dom = new DOMDocument(); 

$dom->loadHTML($source); 
// print_r($dom); 
$xp = new DOMXPath($dom); 

    $textList = $xp->query("//table[//th[contains(text(),'')]]"); 
    foreach ($textList as $text) { 
    $enddate = $xp->evaluate(
       "string(descendant::tr[th[contains(text(),'RIP End Date and Time') or contains(text(),'Output End Date And Time')]]/td/text())", 
       $text); 
     $date = preg_replace("/[^0-9a-zA-Z \/:\-]/", "", $enddate); 
     $xtime = strtotime($date); 
     $tes = date("Y-m-d H:i:s",$xtime); 
     echo "enddate=".$tes.PHP_EOL; 
     } 

OUTPUT:

表1:2017年9月1日13点21分33秒

表2:0000-00-00 00:00:00

回答

0

'29'不是有效的月份值。

看看正在返回值的那个。请注意,它返回的是9月1日,而不是1月9日。

13:21:33 09/01/2017 -> 2017-09-01 13:21:33 
      mm dd yyyy  yyyy mm dd 

然后看看返回零的那个。

22:10:13 29/09/2017  
      mm dd yyyy 

29对于月份不是有效的值。 (如果我们预计这个输入是9月29日的代表,那么我们也可能期望第一个代表1月1日。)

+0

你可以给任何解决方案,我该怎么做.. –

+0

报价:“m/d/y或dmy格式的日期通过查看各个组件之间的分隔符来消除歧义:如果分隔符是斜线(/),则假定为美国m/d/y;” http://php.net/manual/en/function.strtotime.php – spencer7593

+0

是的,应该是2017-01-09,不是2017-09-01 ..日期是错误的.. –

相关问题