Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show closest release date titles using foreach-array in PHP

I'm working on a game release section, where I display games that are about to release. I only work with game information, and a release date.

My array looks like this (actual array has a lot more info so this is just a replicate):

$arr = [
    [
        'id' => 'UP0006-CUSA08724_00-BATTLEFIELDV0000',
        'attributes' => [
            'name' => 'Battlefield V [test1]',
            'thumbnail-url-base' => 'https://store.playstation.com/store/api/chihiro/00_09_000/container/US/en/999/UP0006-CUSA08724_00-BATTLEFIELDV0000/1539651459000/image'
            'release-date' => '2018-12-14T00:00:00Z'
        ],
    ],
    [
        'id' => 'UP0006-CUSA08724_00-BATTLEFIELDV0000',
        'attributes' => [
            'name' => 'Battlefield V [test2]',
            'thumbnail-url-base' => 'https://store.playstation.com/store/api/chihiro/00_09_000/container/US/en/999/UP0006-CUSA08724_00-BATTLEFIELDV0000/1539651459000/image'
            'release-date' => '2018-10-14T00:00:00Z'
        ],
    ],
    [
        'id' => 'UP0006-CUSA08724_00-BATTLEFIELDV0000',
        'attributes' => [
            'name' => 'Battlefield V [test3]',
            'thumbnail-url-base' => 'https://store.playstation.com/store/api/chihiro/00_09_000/container/US/en/999/UP0006-CUSA08724_00-BATTLEFIELDV0000/1539651459000/image'
            'release-date' => '2019-10-14T00:00:00Z'
        ],
    ],
];

I want to display the game titles that are closest to release to the current date such as [test1], and skip the ones that have been released already such as [test2].

I've tried to skip them using this line:

if (strtotime(date('Y-m-d H:i:s')) > strtotime($title['attributes']['release-date'])) continue;

But for some reason it does not seem to skip them, and just keeps them in.

Also I have no idea where to start when trying to show the game titles that are closest to release to the current date.

My full code:

foreach($json['included'] as $key => $title) {
    $cusa = substr(explode('-', $title['id'], 3)[1], 0, -3);

    if($title['type'] == 'game' && substr($cusa, 0, 4) == 'CUSA') {
        // if the day of release has already passed, skip
        if (strtotime(date('Y-m-d H:i:s')) > strtotime($title['attributes']['release-date'])) continue;
            ?>
            <div class="game-banner" style="background:url(<?php echo $title['attributes']['thumbnail-url-base']; ?>)">
                <h4 class="psplus-game-name"><?php echo $title['attributes']['name']; ?></h4>
            </div>
            <?php
            if($key >= 4) break; // display only 3
        }
    }
}
like image 505
Appel Flap Avatar asked Feb 03 '26 14:02

Appel Flap


1 Answers

You just need to calculate the seconds left to release date, and if it's a positive number echo it.

foreach($arr as $game){
    $timeleft = strtotime($game['attributes']['release-date'])-time();
    if($timeleft>0) echo floor($timeleft/86400) ." days left to ".$game['attributes']['name'] ." \n";
}

//58 days left to Battlefield V [test1] 
//362 days left to Battlefield V [test3] 

https://3v4l.org/OMetR

If your initial array is unsorted and you want then sorted you can add them to an array with key being the timeleft and sort on keys with ksort().

foreach($arr as $game){
    $timeleft = strtotime($game['attributes']['release-date'])-time();
    if($timeleft>0) $games[$timeleft] = floor($timeleft/86400) ." days left to ".$game['attributes']['name'] ." \n";
}

ksort($games);
echo implode("", $games);

https://3v4l.org/gbLCs

like image 81
Andreas Avatar answered Feb 06 '26 03:02

Andreas