phpjsonunixepg

PHP: Loop through json comparing current time with time range based on two keys values


I'm basically trying to make an epg viewer based on some json data.

I need some help to figure out how to:

  1. Loop through the json data.
  2. Get the "start" and "stop" values (unix timestamps) of each program.
  3. Make a time range for each program based on the start/stop timestamps.
  4. Compare the time range of each program with the current unix time.
  5. Display the program which has a time range that fits the current time.

This is what the json data looks like:

[
    {
        "id": 1,
        "programs": [
            {
                "stop": 1475982300,
                "start": 1475976300,
                "category": "Serier",
                "id": 356532927,
                "title": "Kommissær Alleyn"
            },
            {
                "stop": 1475983500,
                "start": 1475982300,
                "category": "Børn",
                "id": 355997207,
                "title": "Udsendelsesophør"
            },
            {
                "stop": 1475985000,
                "start": 1475983500,
                "id": 358595605,
                "title": "DR-Friland: Frilandshaven"
            },
            {
                "stop": 1475986800,
                "start": 1475985000,
                "category": "Dokumentar",
                "id": 358595606,
                "title": "Bonderøven 2010"
            },
            {
                "stop": 1475989500,
                "start": 1475986800,
                "category": "Dokumentar",
                "id": 355933560,
                "title": "Made in Denmark"
            },
            {
                "stop": 1475992500,
                "start": 1475989500,
                "category": "Dokumentar",
                "id": 357701600,
                "title": "De forunderlige dyr"
            },
            {
                "stop": 1475995500,
                "start": 1475992500,
                "category": "Dokumentar",
                "id": 358745047,
                "title": "Italien: Fra Sardinien til Abruzzo"
            },
            {
                "stop": 1476001200,
                "start": 1475995500,
                "category": "Serier",
                "id": 356295670,
                "title": "Mord på hjernen"
            },
            {
                "stop": 1476004800,
                "start": 1476001200,
                "category": "Underholdning",
                "id": 357701601,
                "title": "Puk og Herman går i land - Endelave"
            },
            {
                "stop": 1476008100,
                "start": 1476004800,
                "category": "Dokumentar",
                "id": 355933566,
                "title": "Guld på godset"
            },
            {
                "stop": 1476011700,
                "start": 1476008100,
                "category": "Dokumentar",
                "id": 357701602,
                "title": "Guld på godset"
            },
            {
                "stop": 1476017700,
                "start": 1476011700,
                "category": "Serier",
                "id": 356182011,
                "title": "Kriminalkommissær Barnaby"
            },
            {
                "stop": 1476023700,
                "start": 1476017700,
                "category": "Sport",
                "id": 358577056,
                "title": "HåndboldSøndag: HC Motor Zaporozhye-TTH Holstebro (m), direkte"
            },
            {
                "stop": 1476030600,
                "start": 1476023700,
                "category": "Sport",
                "id": 358577057,
                "title": "HåndboldSøndag: Bjerringbro Silkeborg-THW Kiel (m), direkte"
            },
            {
                "stop": 1476032400,
                "start": 1476030600,
                "category": "Nyheder",
                "id": 355933567,
                "title": "TV AVISEN med Sporten og Vejret"
            },
            {
                "stop": 1476032700,
                "start": 1476032400,
                "id": 363167503,
                "title": "Guld i Købstaden - en hurtig handel"
            },
            {
                "stop": 1476036000,
                "start": 1476032700,
                "category": "Dokumentar",
                "id": 359619002,
                "title": "Atlanterhavet: Verdens vildeste hav"
            },
            {
                "stop": 1476039600,
                "start": 1476036000,
                "category": "Serier",
                "id": 355933569,
                "title": "Bedrag II"
            },
            {
                "stop": 1476042000,
                "start": 1476039600,
                "category": "Nyheder",
                "id": 355933570,
                "title": "21 Søndag"
            },
            {
                "stop": 1476042600,
                "start": 1476042000,
                "category": "Sport",
                "id": 358745049,
                "title": "Sporten"
            },
            {
                "stop": 1476049200,
                "start": 1476042600,
                "category": "Film",
                "id": 355933571,
                "title": "Killer Elite"
            },
            {
                "stop": 1476054600,
                "start": 1476049200,
                "category": "Film",
                "id": 356069380,
                "title": "BlackJack"
            },
            {
                "stop": 1476060600,
                "start": 1476054600,
                "category": "Serier",
                "id": 356069381,
                "title": "Taggart: Døden checker ind"
            },
            {
                "stop": 1476066300,
                "start": 1476060600,
                "category": "Film",
                "id": 355933572,
                "title": "Mørkets gerninger"
            },
            {
                "stop": 1476069000,
                "start": 1476066300,
                "category": "Dokumentar",
                "id": 362546476,
                "title": "DR1 Dokumentar: Danskere omringet af IS"
            }
        ]
    }
]

This is the PHP I have so far: (I know it isn't much. I just don't know how to go on from here)

<?php
$channel = '1';
$current_unix = time();
$json = json_decode(file_get_contents('http://tvtid-app-backend.tv2.dk/api/tvtid/v1/dayviews/'.date('Y-m-d').'?ch='.$channel), true);
$programs = $json[0]['programs'];
$programs_length = count($programs);
for ($i = 0; $i < $programs_length ; $i++)
{
    $begin = [$programs[$i]['start']];
    $end = [$programs[$i]['stop']];
    $title = [$programs[$i]['title']];
}
?>

And this how the output should look: (Example time is 16:41)

<ul>
   <li class="program">
      <div class="title_now">TV AVISEN med Sporten og Vejret</div>
      <div class="duration_now">
      <div class="now_begin">16:30</div><span class="begin">1476030600</span>
      <div class="progress_bar"><div class="progress"></div></div>
      <div class="now_end">17:00</div><span class="end">1476032400</span></div>
   </li>
   <li class="program">
      <div class="title">Guld i Købstaden - en hurtig handel</div>
      <div class="duration">17:00-17:05</div>
   </li>
   <li class="program">
      <div class="title">Atlanterhavet: Verdens vildeste hav</div>
      <div class="duration">17:05-18:00</div>
   </li>
   <li class="program">
      <div class="title">Bedrag II</div>
      <div class="duration">18:00-19:00</div>
   </li>

   etc...

</ul>

I hope this makes sense and that someone is able to help me out.


Solution

  • It is far simpler using foreach to process over a complete array.

    $channel = '1';
    $current_unix = time();
    
    // note I have removed the TRUE option from json_decode()
    
    $json = json_decode(file_get_contents('http://tvtid-app-backend.tv2.dk/api/tvtid/v1/dayviews/'.date('Y-m-d').'?ch='.$channel));
    //print_r($json);
    
    echo '<ul>';
    
    foreach ($json as $data) {
    
        echo '<li>';
        foreach ($data->programs as $prog) {
    
            if ( $current_unix < $prog->start ) {
    
                // add your own HTML instead of the simple echo
                echo $prog->title . '<br>';
                echo $prog->start . '<br>';
                echo $prog->stop . '<br>';
                echo $prog->id . '<br>';
                if ( isset($prog->category)) {
                    echo $prog->category . '<br>';
                }
            }
        }
        echo '</li>';
    }
    echo '</ul>';