I'm trying to get the scheadule 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)
CodePudding user response:
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){
$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>";
}
CodePudding user response:
Here is the code I'm using now thanks to you, Jim!
As you can see (screenshot attached), "Hockey Night in Thailand Rewind DEC 15 2019_Game_2 Ep 016" is playing now and I found it in the list here: 22.01.22 : 11:09 - 12:09 - Hockey Night in Thailand Rewind DEC 15 2019_Game_2 Ep 016
<?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>";
}
}
?>
<?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>";
}
Maybe using REMOTE_ADDR will help for get the user location and show the correct user's timezone in the output schedule.
Based on user's location, the date and time in the output would be (Romania timezone GMT 2 - my location):
Start : 13:09 22.01.2022
End : 14:09 22.01.2022
PLAYING NOW : Hockey Night in Thailand Rewind DEC 15 2019_Game_2 Ep 016
NEXT:
22.01.22 : 14:11 - 14:45 - Let's_Go_Thailand_The_Long_Way_to_Bangkok_Ep_016
22.01.22 : 14:47 - 15:14 - The_Tsunami_in_Thailand_2004
22.01.22 : 15:18 - 16:23 - Hockey Night in Thailand Rewind DEC 12 2019_Game_1 Ep 013
22.01.22 : 16:23 - 17:26 - Hockey Night in Thailand Rewind DEC 12 2019_Game_2 Ep 014
22.01.22 : 17:27 - 17:53 - Let's_Go_Thailand_The_Thailand_Project_Ep_013
Ignoring also all those fillers from the output (ex: ThailandTV_Filler_1B).
Thanks again, Ady
