<?php function generateAffiliateCommands(int $companyId, int $daysBack) { $sections = [ 'AFFILIATES' => [ "affiliate:cac-sync-affiliates %d", "affiliate:cac-sync-commission-plan-reports %d" ], 'COMMISSIONS' => [ "affiliate:cac-sync-commissions %d --dateFrom=\\\"%s\\\" --dateTo=\\\"%s\\\"" ], 'REGISTRATIONS' => [ "affiliate:cac-sync-registrations %d --dateFrom=\\\"%s\\\" --dateTo=\\\"%s\\\"" ], 'TRANSACTIONS' => [ "affiliate:cac-sync-transactions %d --dateFrom=\\\"%s\\\" --dateTo=\\\"%s\\\"" ] ]; $commands = []; // AFFILIATES section $commands[] = "## AFFILIATES ##"; $affiliateCmds = []; foreach ($sections['AFFILIATES'] as $index => $cmd) { $formatted = sprintf($cmd, $companyId); if ($index > 0) { $formatted = "php artisan $formatted"; } $affiliateCmds[] = $formatted; } $commands[] = implode(' && ', $affiliateCmds); // Generate 30-day periods starting from -1 backwards $periods = []; $chunkSize = 30; $currentTo = -1; while (abs($currentTo) < $daysBack) { $currentFrom = $currentTo - $chunkSize + 1; // Ensure we don’t go beyond -daysBack if (abs($currentFrom) > $daysBack) { $currentFrom = -$daysBack; } $periods[] = [$currentFrom, $currentTo]; // Next chunk ends where this one starts $currentTo = $currentFrom - 1; } // Flip to oldest first (-daysBack → -1) $periods = array_reverse($periods); // Generate other sections foreach (['COMMISSIONS', 'REGISTRATIONS', 'TRANSACTIONS'] as $section) { $commands[] = "## $section ##"; $sectionCmds = []; foreach ($periods as $i => [$from, $to]) { if ($to !== -1) { $to = $to + 1; } $cmd = sprintf($sections[$section][0], $companyId, "$from days", "$to days"); if ($i > 0) { $cmd = "php artisan $cmd"; } $sectionCmds[] = $cmd; } $commands[] = implode(' && ', $sectionCmds); } return implode("\n\n", $commands); } function dateToDaysAgo(string $date) { // Create DateTime objects $given = new DateTime($date); $now = new DateTime(); // current date and time // Calculate difference $diff = $now->diff($given); // Total days difference $daysAgo = $diff->days; // If the given date is in the past, return negative if ($given < $now) { return $daysAgo + 1; } else { return -$daysAgo - 1; } } $companyId = 223; $date = '2024-11-13 15:44:50'; echo generateAffiliateCommands($companyId, dateToDaysAgo($date));
You have javascript disabled. You will not be able to edit any code.