douhan1992 2015-06-16 17:32 采纳率: 100%
浏览 74

将XML日期提取到PHP

I have an XML document with an entry looking like this:

<recall-number id="F-2402-2015">
     <product-type>Food</product-type>
     <event-id>70195</event-id>
     <status>Ongoing</status>
     <recalling-firm>Northhampton Peanut Company</recalling-firm>  
     <city>Severn</city>
     <state>NC</state>
     <country>US</country>
     <voluntary-mandated>Voluntary: Firm Initiated</voluntary-mandated>
     <initial-firm-notification>Letter</initial-firm-notification>
     <distribution-pattern>CT, MA, NJ, NY and PA.</distribution-pattern>
     <classification>Class II</classification>  
     <product-quantity>unknown</product-quantity> 
     <reason-for-recall>Undeclared Non-Allergen: Sulfites</reason-for-recall> 
     <recall-initiation-date>12/24/2014</recall-initiation-date>
     <report-date>06/10/2015</report-date>
</recall-number>

I'm using simplexml_load_file and am able to obtain all of the fields, however the two date fields at the end (recall-initiation-date/report-date) are showing up blank. After some research I used the date function:

function get_recalls() {
     $url = "Enforcement-Reports-Export.xml";
     $xml = simplexml_load_file($url);
     foreach($xml->{'recall-number'} as $product) {
          $recall_initiation_date = date('d/m/Y', $product{'recall-initiation-date'});
          $report_date = date('d/m/Y', $product{'report-date'});

          echo $recall_initiation_date;
          echo $report_date;
     }
}

However the date that it returns is 01/01/1970 and not the correct date.

  • 写回答

1条回答 默认 最新

  • dongyouzhi7218 2015-06-16 17:33
    关注

    Because date() expects a Unix timestamp as a parameter and you are giving it a string. Use strtotime() to convert those dates into a Unix timestamp.

    $recall_initiation_date = date('d/m/Y', strtotime($product{'recall-initiation-date'}));
    $report_date = date('d/m/Y', strtotime($product{'report-date'}));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题