pull-icon
logo-mobile

Rotation - PHP Rotation Script - 30d / 3h / 5h

Home/Forums/Rotation - PHP Rotation Script - 30d / 3h / 5h

Bronze Member
Bronze Member

SCS-BOT

last month

This rotation was automatically generated using the StreamCode Studio Rotation Builder.

Below is a detailed summary of the settings that were applied during the build process.
These values are for an artist not played for a minimum of 30 days, Not repeating Artist for 3 hours with a 5 hour rotation.

Rotation Settings Used:
Output type: PHP Rotation Script
- Days since last play: 30
- Artist separation: 3 hours
- Target duration: 5 hours


<?php
declare(strict_types=1);

/**
 * Standalone RadioDJ rotation builder
 * -----------------------------------
 * Filters songs by:
 * - days since last play
 * - avoids repeated artists
 * - targets total playlist duration
 */

date_default_timezone_set('Europe/London');

$dsn  = 'mysql:host=127.0.0.1;port=3306;dbname=radiodj;charset=utf8mb4';
$user = 'radiodj_user';
$pass = 'radiodj_pass';

try {
    $pdo = new PDO($dsn, $user, $pass, [
        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    ]);
} catch (Throwable $e) {
    die('Database connection failed: ' . $e->getMessage());
}

$TARGET_DURATION = 18000;
$DAYS_BACK       = 30;
$MIN_DURATION    = 30;

$sql = "
    SELECT id, artist, title, duration, path, weight
    FROM songs
    WHERE enabled = 1
      AND (date_played IS NULL OR date_played < NOW() - INTERVAL :days DAY)
    ORDER BY (weight + 1) * RAND()
    LIMIT 2000
";

$stmt = $pdo->prepare($sql);
$stmt->execute([':days' => $DAYS_BACK]);
$rows = $stmt->fetchAll();

$playlist      = [];
$totalDuration = 0;
$usedArtists   = [];

foreach ($rows as $row) {
    $artist   = trim($row['artist']);
    $title    = trim($row['title']);
    $duration = (int) $row['duration'];
    $path     = trim($row['path']);

    if ($artist === '' || $title === '' || $path === '') continue;
    if ($duration < $MIN_DURATION) continue;
    if (isset($usedArtists[$artist])) continue;
    if ($totalDuration + $duration > $TARGET_DURATION) continue;

    $playlist[] = $row;
    $usedArtists[$artist] = true;
    $totalDuration += $duration;
}

$outputFile = __DIR__ . '/rotation_auto.m3u';
$fh = fopen($outputFile, 'wb');
fwrite($fh, "#EXTM3U
");

foreach ($playlist as $track) {
    fwrite($fh, "#EXTINF:{$track['duration']}," . $track['artist'] . " - " . $track['title'] . "\r
");
    fwrite($fh, $track['path'] . "\r
");
}

fclose($fh);

echo "Playlist built: " . count($playlist) . " tracks\n";
If you have any suggestions, improvements, or want to share your own version, feel free to comment below.
You can also regenerate or tweak this rotation any time using the StreamCode Studio Rotation Helper Tool.

Leave a Comment

You must be logged in to post a reply.

Thank you ??

Thanks for supporting this station!

We've sent a download link to your email {}
Please check your inbox (and spam folder just in case).
The link will be valid for 24 hours.



BellOnline offers fast, reliable UK hosting with strong security and excellent uptime - perfect for modern radio and web projects.
MixStream provides broadcast-grade streaming with crystal-clear audio and rock-solid stability, ideal for both hobby and professional stations.

Goto BellOnline Now!

Close