计算两个时间戳之间的时间量

时间:2013-10-28 10:44:27

标签: php xml

XML文件;

 <programme channel="cnn.info" start="20131027060000" stop="20131027061500">
 <title>hello CONTENT</title>
 <premiere/></programme>

“开始”&amp;应该用“停止”(在xml中)来计算时钟之间的时差吗?

例如:

Title: hello.. 
Time:left 24 minutes
用数据计算xml文件的时间,怎么办?

2 个答案:

答案 0 :(得分:1)

应该针对这样的事情:

$string = <<<XML
 <programme channel="cnn.info" start="20131027060000" stop="20131027061500">
 <title>hello CONTENT</title>
 <premiere/></programme>
XML;

$xml = simplexml_load_string($string);

$start = $xml->attributes()->start;
$stop = $xml->attributes()->stop;
$title = $xml->title;
$datetime1 = new DateTime($start);
$datetime2 = new DateTime($stop);
$interval = $datetime1->diff($datetime2);
echo "Title: " . $title . "\n";
echo $interval->format('Time left: %i minutes');

输出:

Title: hello CONTENT
Time left: 15 minutes

http://3v4l.org/gDPTQ

答案 1 :(得分:0)

$xml = simplexml_load_file("filename.xml");

echo $xml->programme->title . "<br />";
echo "Time: left" . $time_left;

$time_left将是停止日期减去开始日期,然后将其转换为php日期。

同时阅读本主题: Php get how many days and hours left from a date