【问题标题】:Get schedule for today, current time +10 hours parsing xml获取今天的时间表,当前时间+10小时解析xml
【发布时间】:2022-01-28 20:23:05
【问题描述】:

我正在尝试根据访问者的“时区”从此 XML 文件中获取“当前”日期和时间的时间表:

<programme start="20220120070000 +0000" stop="20220120080251 +0000" channel="33">
<title lang="en">
Hockey Night in Thailand Nov 17 2019_Game_1 Eps 005
</title>
<sub-title lang="en">
Hockey Night in Thailand Nov 17 2019_Game_1 Eps 005
</sub-title>
<desc lang="en">
Encore Presentation of the Siam Hockey League (SHL) from Bangkok Thailand
</desc>
<date>20220120</date>
<category lang="en">Featured</category>
<category lang="en">Airy TV 1</category>
</programme>

到目前为止,我已经设法获得完整的输出或前 10 行或“正在播放”,但我不知道如何限制当天的结果,访问者的时区 +10 小时(例如)。

我想要的只是得到以下 15-20 行的输出,仅此而已,也忽略“填充”。

PLAYING NOW : Hockey Night in Thailand Rewind Dec_1_2019_Game_1 Ep 011
NEXT: 
22.01.21 : 10:37 - 11:03 - Let's_Go_Thailand_Sights_and_Sounds_Ep_010
22.01.21 : 11:03 - 11:06 - ThailandTV_Filler_1F
22.01.21 : 11:06 - 11:35 - Let's_Go_Thailand_Sights_and_Sounds_Bangkok_Ep_011
22.01.21 : 11:35 - 11:38 - ThailandTV_Filler_1H
22.01.21 : 11:38 - 12:05 - Let's_Go_Thailand_Sights_and_Sounds_Ep_012
22.01.21 : 12:05 - 12:55 - Going_Astray_(11_screen_version)  ...

任何帮助将不胜感激!

谢谢! 阿迪

使用的代码:

前 10 行

<?php    
    session_start();
    $timezone = $_SESSION['time'];
$line = 0 ;
$xml=simplexml_load_file("https://hls.airy.tv/airytv/33");
        echo "<br>";
foreach($xml->programme as $item){
    if ($line++ == 10) break;
    echo " " .date("y.m.d", strtotime(substr($item["start"], 0,  -6))); 
    echo " : " .date("G:i", strtotime(substr($item["start"], 0,  -6)));
    echo " - " .date("G:i ", strtotime(substr($item["stop"], 0,  -6)));
    echo " - ".$item->title;
    echo "<br>";
}
?> 

输出:

22.01.20 : 7:00 - 8:02 - Hockey Night in Thailand Nov 17 2019_Game_1 Eps 005
22.01.20 : 8:02 - 8:04 - ThailandTV_Filler_1A
22.01.20 : 8:04 - 9:03 - Hockey Night in Thailand Nov 17 2019 Game 2 Eps 006
22.01.20 : 9:03 - 9:04 - ThailandTV_Filler_1B
22.01.20 : 9:04 - 9:33 - Let's_Go_Thailand_Motorcycles,_Mr._Cartoon_Ep_005
22.01.20 : 9:33 - 10:00 - Let's_Go_Thailand_Amazing_Elephants_Ep_006
22.01.20 : 10:00 - 10:03 - ThailandTV_Filler_1F
22.01.20 : 10:03 - 11:03 - Hockey Night in Thailand Nov 21 2019_Game 1 Eps 007
22.01.20 : 11:03 - 11:04 - ThailandTV_Filler_1A
22.01.20 : 11:04 - 12:02 - Hockey Night in Thailand Nov 21 2019 Game 2 Eps 008 

现在播放

<?php    
$channels = array();
foreach ($xml->channel as $c) {
    $channels[ $c['id']->__toString() ] = $c->{'display-name'}->__toString();
}
$time = date( "YmdHi" );
$xml=simplexml_load_file("https://hls.airy.tv/airytv/33");
foreach($xml->programme as $item) {

    $start = substr( (string)$item["start"], 0, -8);
    $end   = substr( (string)$item["stop"], 0, -8);
    if ($time > $start && $time < $end) {
//        echo "Start : " .date("G:i d.m.Y", strtotime($start)) . '<br>';
//        echo "End : " .date("G:i d.m.Y", strtotime($end)) . '<br>';
        echo "PLAYING NOW : ".$item->title. "<br>";
}
}
?> 

输出

PLAYING NOW : Hockey Night in Thailand Rewind Dec_1_2019_Game_1 Ep 011 

完整的时间表

<?php    
    session_start();
    $timezone = $_SESSION['time'];
$i = 0;
$xml=simplexml_load_file("https://hls.airy.tv/airytv/33");
        echo "<br>";
foreach($xml->programme as $item){
    echo " " .date("y.m.d", strtotime(substr($item["start"], 0,  -6))); 
    echo " : " .date("G:i", strtotime(substr($item["start"], 0,  -6)));
    echo " - " .date("G:i ", strtotime(substr($item["stop"], 0,  -6)));
    echo " - ".$item->title;
    echo "<br>";
}
?>

输出

22.01.20 : 7:00 - 8:02 - Hockey Night in Thailand Nov 17 2019_Game_1 Eps 005
22.01.20 : 8:02 - 8:04 - ThailandTV_Filler_1A
22.01.20 : 8:04 - 9:03 - Hockey Night in Thailand Nov 17 2019 Game 2 Eps 006
22.01.20 : 9:03 - 9:04 - ThailandTV_Filler_1B
22.01.20 : 9:04 - 9:33 - Let's_Go_Thailand_Motorcycles,_Mr._Cartoon_Ep_005
22.01.20 : 9:33 - 10:00 - Let's_Go_Thailand_Amazing_Elephants_Ep_006
22.01.20 : 10:00 - 10:03 - ThailandTV_Filler_1F
22.01.20 : 10:03 - 11:03 - Hockey Night in Thailand Nov 21 2019_Game 1 Eps 007
22.01.20 : 11:03 - 11:04 - ThailandTV_Filler_1A
22.01.20 : 11:04 - 12:02 - Hockey Night in Thailand Nov 21 2019 Game 2 Eps 008
22.01.20 : 12:02 - 12:05 - ThailandTV_Filler_1H
22.01.20 : 12:05 - 12:33 - Let's_Go_Thailand_Chiang_Mai._Ep_007
22.01.20 : 12:33 - 12:34 - ThailandTV_Filler_1A
22.01.20 : 12:34 - 13:01 - Let's_Go_Thailand_Amazing_Elephants_Ep_006
22.01.20 : 13:01 - 13:07 - ThailandTV_Filler_1N
... (a very long list) 

【问题讨论】:

  • 不确定是否可以使用您当前的信息完成。 $_SESSION['time'] 的值是多少?那里可能有也可能没有足够的信息来将程序限制在用户的时区。

标签: php xml date schedule


【解决方案1】:

如何从结果中删除包含“Filler”的标题?

更新代码

<?php 

// session_start();
// $timezone = $_SESSION['time']; // What format is this variable?
// $i = 0;
$xml=simplexml_load_file("https://hls.airy.tv/airytv/33");

// Placeholder for user's date.
$userDt = new DateTimeImmutable();
// Get user's start and end of day.
$userStartDt = $userDt->modify('00:00:00');
$userEndDt = $userDt->modify('23:59:59');

echo "<br>";

/** 
 * XML start & end times are in the format of 4 year digits,
 * 2 month digits, 2 day digits, 2 twenty-four hour digits,
 * 2 minute digits, 2 second digits, a space, a plus or minus
 * sign followed by the timezone offset.
**/
define('DT_XML_FORMAT', 'YmdHis T');

foreach($xml->programme as $item){

    $startDt = DateTimeImmutable::createFromFormat(DT_XML_FORMAT, (string) $item["start"]);
    $endDt = DateTimeImmutable::createFromFormat(DT_XML_FORMAT, (string) $item["stop"]);

    // Check if the program's start or end time lies within
    // the user's current day. If not, skip to the next entry.
    if(
        ($startDt < $userStartDt && $endDt < $userStartDt) ||
        $startDt > $userEndDt
    ) {
        continue;
    }

//    echo " " . $startDt->format("y.m.d"); 
//    echo " : " . $startDt->format("G:i");
//    echo " - " . $endDt->format("G:i");
    echo "".$item->title;
    echo "<br>";

}
?>

输出:

Hockey Night in Thailand Nov 14 2019 Game 2 Eps 004
ThailandTV_Filler_1C <<<<<<<<<<<<<<<<<<<<<<<<<
Let's_Go_Thailand_Sights_and_Sounds_Ep_003
Let's_Go_Thailand_Sights_and_Sounds_Ep_002
ThailandTV_Filler_1H <<<<<<<<<<<<<<<<<<<<<<<<<
The_Coin_short_movie
Hockey Night in Thailand Nov 7 2019 Game 1 Eps 001
ThailandTV_Filler_1A <<<<<<<<<<<<<<<<<<<<<<<<<
Hockey Night in Thailand NOV 7 2019 Game 2 Eps 002
...

再次感谢, 阿迪

【讨论】:

  • 获取用户时区的方法有多种。一旦您以 IANA 或偏移格式获取它,您就可以将其传递给第二个 $userDt 参数(例如:$userDt = new DateTimeImmutable('now', new DateTimeZone('Europe/Bucharest'));)。第一个参数 (now) 是 strtotime 可读格式的实际用户时间。
  • 此外,一旦您获得他们的时区,您就可以将开始和结束程序的日期时间修改为用户的时区,以便它显示用户的时间,而不是 XML 服务器呈现的 GMT。
  • 谢谢!这正是我一直在寻找的。您能否帮我编写代码,以便根据用户的时区获取开始和结束程序的日期时间。另外,我想从输出中删除所有这些“填充器”。再次感谢!
  • 有不同的方法可以做到这一点。您可以使用地理定位 IP API,并可能添加时区 API 调用(有许多免费和付费选项可用)。一种免费的方法是使用 JavaScript INTL 库:stackoverflow.com/a/44935836/1456201(它在所有现代浏览器中都受支持,但 IE 不支持,并且您不会在第一次请求时获得它。您需要一个脚本来设置具有该值的 cookie然后重新加载或先导航到另一个页面)。
  • 你好吉姆!在这种情况下,我想我将只使用“PLAYING NOW”(这个运行良好)并在下面添加“NEXT”并只取程序后面的标题,比如说接下来的 20 行,不包括包含的标题“填料”。请检查我在主题中的最后一个答案。
【解决方案2】:

在这种情况下,我想我只需要“PLAYING NOW”(这个运行良好)并在下面添加“NEXT”,并且只取程序后面的标题,比如说接下来的 20 行,不包括包含“Filler”的标题。

输出(示例)

PLAYING NOW : Hockey Night in Thailand Rewind DEC 15 2019_Game_2 Ep 016
NEXT:
Let's_Go_Thailand_The_Long_Way_to_Bangkok_Ep_016
The_Tsunami_in_Thailand_2004
Hockey Night in Thailand Rewind DEC 12 2019_Game_1 Ep 013
Hockey Night in Thailand Rewind DEC 12 2019_Game_2 Ep 014
Let's_Go_Thailand_The_Thailand_Project_Ep_013 
...

你能改进下面的代码吗?

<?php    
$channels = array();
foreach ($xml->channel as $c) {
    $channels[ $c['id']->__toString() ] = $c->{'display-name'}->__toString();
}
$time = date( "YmdHi" );
$xml=simplexml_load_file("https://hls.airy.tv/airytv/33");
foreach($xml->programme as $item) {

    $start = substr( (string)$item["start"], 0, -8);
    $end   = substr( (string)$item["stop"], 0, -8);
    if ($time > $start && $time < $end) {
//        echo "Start : " .date("G:i d.m.Y", strtotime($start)) . '<br>';
//        echo "End : " .date("G:i d.m.Y", strtotime($end)) . '<br>';
        echo "PLAYING NOW : ".$item->title. "<br>";
        echo "NEXT : " . "<br>";
}
}
?>

或者...回到“FULL SCHEDULE”代码,如何从结果中删除包含“Filler”的标题?

<?php 

// session_start();
// $timezone = $_SESSION['time']; // What format is this variable?
// $i = 0;
$xml=simplexml_load_file("https://hls.airy.tv/airytv/33");

// Placeholder for user's date.
$userDt = new DateTimeImmutable();
// Get user's start and end of day.
$userStartDt = $userDt->modify('00:00:00');
$userEndDt = $userDt->modify('23:59:59');

echo "<br>";

/** 
 * XML start & end times are in the format of 4 year digits,
 * 2 month digits, 2 day digits, 2 twenty-four hour digits,
 * 2 minute digits, 2 second digits, a space, a plus or minus
 * sign followed by the timezone offset.
**/
define('DT_XML_FORMAT', 'YmdHis T');

foreach($xml->programme as $item){

    $startDt = DateTimeImmutable::createFromFormat(DT_XML_FORMAT, (string) $item["start"]);
    $endDt = DateTimeImmutable::createFromFormat(DT_XML_FORMAT, (string) $item["stop"]);

    // Check if the program's start or end time lies within
    // the user's current day. If not, skip to the next entry.
    if(
        ($startDt < $userStartDt && $endDt < $userStartDt) ||
        $startDt > $userEndDt
    ) {
        continue;
    }

//    echo " " . $startDt->format("y.m.d"); 
//    echo " : " . $startDt->format("G:i");
//    echo " - " . $endDt->format("G:i");
    echo "".$item->title;
    echo "<br>";

}
?>

输出:

Hockey Night in Thailand Nov 14 2019 Game 2 Eps 004
ThailandTV_Filler_1C <<<<<<<<<<<<<<<<<<<<<<<<<
Let's_Go_Thailand_Sights_and_Sounds_Ep_003
Let's_Go_Thailand_Sights_and_Sounds_Ep_002
ThailandTV_Filler_1H <<<<<<<<<<<<<<<<<<<<<<<<<
The_Coin_short_movie
Hockey Night in Thailand Nov 7 2019 Game 1 Eps 001
ThailandTV_Filler_1A <<<<<<<<<<<<<<<<<<<<<<<<<
Hockey Night in Thailand NOV 7 2019 Game 2 Eps 002
...

非常感谢您的宝贵支持!

阿迪

【讨论】:

  • 您可以使用我给出的示例来改进您的代码。如果你遇到问题,你可以提出一个新问题(这个问题已经超出了主题)。我不知道你说的填充物是什么意思。
  • 示例:ThailandTV_Filler_1H
  • 您是要完全删除这些条目(跳过它们)还是以某种方式修改它们?
  • 是的,删除/跳过。
  • 查看我的答案,我更新了它,跳过了填充内容。
【解决方案3】:

您可以通过查看开始日期和结束日期并比较开始日期或结束日期是否在用户当前日期内,将条目限制在特定日期。使用DateTime 对象 - 它将跟踪提供的时区,以便可以正确地修改到另一个时区。请注意,DateTimeImmutable 只是一个不可更改的 DateTime 对象。

我在当前服务器时间放了一个占位符。如果您可以提供足够的信息来利用用户的时区,我可能会提供进一步的帮助(请参阅我对您帖子的评论)。

<?php 

session_start();
$timezone = $_SESSION['time']; // What format is this variable?
$i = 0;
$xml=simplexml_load_file("https://hls.airy.tv/airytv/33");

// Placeholder for user's date.
$userDt = new DateTimeImmutable();
// Get user's start and end of day.
$userStartDt = $userDt->modify('00:00:00');
$userEndDt = $userDt->modify('23:59:59');

echo "<br>";

/** 
 * XML start & end times are in the format of 4 year digits,
 * 2 month digits, 2 day digits, 2 twenty-four hour digits,
 * 2 minute digits, 2 second digits, a space, a plus or minus
 * sign followed by the timezone offset.
**/
define('DT_XML_FORMAT', 'YmdHis T');

foreach($xml->programme as $item){

    // Skip entries that contain the word "filler"
    if(preg_match('/[\b_]filler[\b_]/i', (string) $item->title)) {
        continue;
    }

    $startDt = DateTimeImmutable::createFromFormat(DT_XML_FORMAT, (string) $item["start"]);
    $endDt = DateTimeImmutable::createFromFormat(DT_XML_FORMAT, (string) $item["stop"]);

    // Check if the program's start or end time lies within
    // the user's current day. If not, skip to the next entry.
    if(
        ($startDt < $userStartDt && $endDt < $userStartDt) ||
        $startDt > $userEndDt
    ) {
        continue;
    }

    echo " " . $startDt->format("y.m.d"); 
    echo " : " . $startDt->format("G:i");
    echo " - " . $endDt->format("G:i");
    echo " - ".$item->title;
    echo "<br>";

}

【讨论】:

  • 嗨,吉姆,非常感谢您的回答!
  • 添加检查以跳过填充内容。
  • 现在完美了!非常感谢吉姆!
猜你喜欢
  • 2014-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-06
  • 2021-04-14
  • 2011-07-06
  • 2019-10-30
相关资源
最近更新 更多