0

I'm trying to get the schedule for "current" date and time based on visitor's "timezone" from this XML file:

<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>

So far I have managed to get the full output or just the first 10 lines or "playing now" but I don't know how I could restrict the results for current day, visitor's timezone +10 hours (for example).

All I want is to get the following output 15-20 lines, no more, ignoring the "Filler" also.

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)  ...

Any help will be much appreciated!

Thanks! Ady

Code used:

For first 10 lines

<?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>";
}
?> 

Output:

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 

For playing NOW

<?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>";
}
}
?> 

Output

PLAYING NOW : Hockey Night in Thailand Rewind Dec_1_2019_Game_1 Ep 011 

For FULL SCHEDULE

<?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>";
}
?>

Output

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) 
adydeejay
  • 1
  • 1
  • Not sure if it can be done with your current information. What is the value of `$_SESSION['time']`? There may or may not be enough information there to restrict the programs to the user's timezone. – Jim Jan 22 '22 at 01:44

3 Answers3

0

You can restrict the entries to a specific day by looking at the start and end dates and comparing to see if the start or end date lies within the user's current day. Use the DateTime object - it will keep track of the provided timezone so that modification to another timezone can be done properly. Note that DateTimeImmutable is just a non-alterable DateTime object.

I put a placeholder there with the current server time. If you can provide enough information to utilize the user's timezone I may be able to assist further (see my comment on your post).

<?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>";

}
Jim
  • 3,210
  • 2
  • 17
  • 23
0

How can I remove titles that contain "Filler" from results?

UPDATED CODE

<?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>";

}
?>

Output:

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
...

Thanks again, Ady

adydeejay
  • 1
  • 1
  • There are different ways to get user timezone. Once you get it in IANA or offset format you can pass it into the second `$userDt` parameter (ex: `$userDt = new DateTimeImmutable('now', new DateTimeZone('Europe/Bucharest'));`). The first parameter (`now`) is the actual user's time in a strtotime-readable format. – Jim Jan 22 '22 at 12:29
  • Also once you get their timezone you could modify the start and end program datetimes to the user's timezone so that it will show the user's time rather than GMT that the XML server is presenting. – Jim Jan 22 '22 at 12:31
  • Thanks! This is exactly I was looking for. Could you please help me with the code in order to get the start and end program datetimes based on the user's timezone. Also, I would like to remove all these "Filler"s from the output. Thanks again! – adydeejay Jan 22 '22 at 13:16
  • There are different ways to do that. You could use a geolocation IP API possibly with the addition of a time zone API call (there are a number of free and paid options available). A free way would be to use the JavaScript INTL library: https://stackoverflow.com/a/44935836/1456201 (It is supported in all modern browsers but not IE, and you would not get it on the first request. You would need a script to set a cookie with the value then reload or navigate to another page first). – Jim Jan 23 '22 at 16:34
  • Hello Jim! In this case, I think I'll just take "PLAYING NOW" (this one it's working well) and add "NEXT" underneath and take only the titles that follow from the program, lets say the next 20 lines, excluding the titles containing "Filler". Please check my last answer from the main topic. – adydeejay Jan 24 '22 at 19:40
  • Hello Jim! How can I remove titles that contain "Filler" from results? – adydeejay Jan 26 '22 at 18:33
  • What do you mean by filler? – Jim Jan 27 '22 at 14:26
  • Example: ThailandTV_Filler_1H <<< "filler" in the title = promotional materials between the episodes. – adydeejay Jan 27 '22 at 20:29
  • See my answer, I updated it with a skip for filler content. – Jim Jan 28 '22 at 12:23
0

In this case, I think I'll just take "PLAYING NOW" (this one it's working well) and add "NEXT" underneath and take only the titles that follow from the program, lets say the next 20 lines, excluding the titles containing "Filler".

Output (example)

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 
...

Could you please improve the code below?

<?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>";
}
}
?>

Or ... back to the "FULL SCHEDULE" code, how can I remove titles that contain "Filler" from results?

<?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>";

}
?>

Output:

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
...

Thank you very much for your valuable support!

Ady

adydeejay
  • 1
  • 1
  • You can use the example I gave to improve your code. If you run into issues you can make a new question (this one is getting over-run and off-topic). I don't know what you mean by filler. – Jim Jan 27 '22 at 14:27
  • Example: ThailandTV_Filler_1H <<< "filler" in the title = promotional materials between the episodes. So it's about removing the lines containing "Filler" in the title. – adydeejay Jan 27 '22 at 20:20
  • Are you looking to remove these entries entirely (skip over them) or modify them in some way? – Jim Jan 27 '22 at 21:26
  • Yes, to remove/skip. – adydeejay Jan 27 '22 at 22:03
  • See my answer, I updated it with a skip for filler content. – Jim Jan 28 '22 at 12:24