我正在编写一个 PHP 脚本来解析从我们公司使用的软件生成的 XML 文件。时间格式就是这样格式化的;我想知道他们是否是解析时间的更简单方法。
<duration>PT1H23M5S</duration> // This is the XML format
我有一个脚本来解析它,但我觉得它可能太长而且比它需要的更复杂:
if( strpos($duration, "H") == false ) {
$duration = substr_replace($duration, "00:", 0, 0);
}
$duration = preg_replace("#PT#", "", $duration);
$duration = preg_replace("#H#", ":", $duration);
$duration = preg_replace("#M#", ":", $duration);
$duration = preg_replace("#S#", "", $duration);
// Prepare to process values under 10 (add trailing 0)
list($hour, $minute, $second) = explode(":", $duration);
if($hour < 10 and $hour != "00") {
$hour = substr_replace($hour, "0", 0, 0); // adds a 0 behind digit EG 09 H
}
if($minute < 10) {
$minute = substr_replace($minute, "0", 0, 0);
}
if($second < 10 and $second > 0) {
$second = substr_replace($second, "0", 0, 0);
}
if($second == "") {
$second = "00";
}
$duration = $hour . ":" . $minute . ":" . $second;
提前感谢您的任何回复!