从字符串解析日期

时间:2011-12-02 08:22:00

标签: php parsing

我正在使用DOMxPath从xml文件中检索元素内的数据......

$xml = file_get_contents('data.xml');
$dom = new DOMDocument();
@$dom->loadHTML($xml);
$domx = new DOMXPath($dom);
$entries = $domx->evaluate("//observation_time");


$arr = array();
foreach ($entries as $entry) {

  $ar6 = $entry->nodeValue;

}


echo "$ar6";

}

这是xml

<observation_time>Last Updated on Dec 1 2011, 6:47 pm EST</observation_time>

输出结果为:最后更新于2011年12月1日,美国东部时间下午6:47

我的问题是:我如何只拉出这个字符串的日期?使用find?

1 个答案:

答案 0 :(得分:1)

通过此输入,您可以执行以下操作:

$time = 'Last Updated on Dec 1 2011, 6:47 pm EST';

#following line will give you date part only
$time = preg_replace('~^Last Updated on\s*~i', '', $time);

#this line will convert time to unix timestamp
$time = strtotime($time);