Task scheduler like cron jobs

Hello,

The code below works like a cron job.
You can copy it to a page and test it.

Rules
When the day or days of the week are selected, the day of the month is disabled.
Selecting * asterisk in the day of the week field disables the day of the week and enables the day of the month.

When * asterisk is selected in the day field, it means every day.
When an asterisk * is selected in the hour field, it means every hour.
When an asterisk * is selected in the minute field, it means every minute.

It sets the time according to the values selected in the day, hour and minute field.
If it is today, it sets the current hour or the next time if minutes have passed.

So, what do I intend to do with this?
I want to place a menu on my web page and run multiple tasks.
With add task I will save the data and unix timestamp to the database and the task whose due date is like ‘time() >= $row’ will be run and update the unix timestamp of the next time.
I will add a cron job from cPanel that will run every minute.

I would like to correct any mistakes, missing codes, shorter codes, more efficient, faster, support for PHP 8 or higher versions in my code below.

<?php 


if($_SERVER['REQUEST_METHOD'] == 'POST'){
echo '<pre>' . print_r($_POST, true) . '</pre>';

$gun = $_POST['gun']; // Day
$saat = $_POST['saat']; // Hour
$dakika = $_POST['dakika']; // Minute
$haftanin_gunu = isset($_POST['haftanin_gunu']) ? $_POST['haftanin_gunu'] : [0=>-1]; // Day(s) of the week


// Get current date and time information
$bugun = new DateTime();

// Create default date object to store Unix timestamp
$tarih = new DateTime();

##############################################################################################################################################################
//if (strpos($saat, '/') == false) {

// Days of the week control
if (in_array("-1", $haftanin_gunu)) {

// If an asterisk is selected on the days of the week, we will use the days of the month as the days of the week are disabled.
##############################################################################################################################################################
    // Gün kontrolü
    if ($gun == -1) {
        // If day * (asterisk) is selected, set the day to tomorrow
        $ayin_son_gunu = (int)$bugun->format('t'); // Get the last day of the month
        $yarinin_gunu = $bugun->format('d') + 1; // Take tomorrow's day

        if(strpos($saat, '/') == false && $saat != -1 && $bugun->format('i') > $dakika && $bugun->format('H') > $saat){
            if ($yarinin_gunu > $ayin_son_gunu) {
                $tarih->modify('first day of next month'); // If tomorrow is greater than the last day of the month, take the first day of the next month
            } else {
                $tarih->modify('+1 day'); // If tomorrow is not older than the last day of the month, take the next day
            }
        }

    } else {
        // If a specific day is selected
        // Determine the time by taking into account Day, Hour and Minute
        $bugunun_gunu = $bugun->format('d');
        $bugunun_saati = $bugun->format('H');
        $bugunun_dakikasi = $bugun->format('i');
        // if rules
        // If the specified day is less than today's day, give the next month
        // If the specified day is equal to today's day and the specified time is less than today's time, give the next month.
        // If the specified day is equal to today's day, the specified hour is equal to today's time, the specified minute is less than or equal to today's minute, give the next month.
        if ($bugunun_gunu > $gun 
        || ($bugunun_gunu == $gun && ($saat > -1 && $saat < $bugunun_saati 
        || ($saat > -1 && $saat == $bugunun_saati && $dakika > -1 && $dakika <= $bugunun_dakikasi)))) {
            
            $tarih->setDate($tarih->format('Y'), $tarih->modify('first day of next month')->format('m'), $gun); // Set Day of next month
        }else{
            // Since either Hour or Minute or both * asterisk -1 is selected, we only get the year, month and day from here.
            // Hour and Minute operations will be set in the $hour and $minute field below
            $tarih->setDate($tarih->format('Y'), $tarih->format('m'), $gun);
            
        }
    }
##############################################################################################################################################################
}else{ // Since the day or days of the week are selected, we will process according to the day of the week.

// Days of the week control

        $bugunun_gunu = $bugun->format('N');
        $bugunun_saati = $bugun->format('H');
        $bugunun_dakikasi = $bugun->format('i');
        // If the result is not reached in the foreach loop, return the first day in the series.
        $h_gunu_ver = $haftanin_gunu[0];

        $haftanin_isimleri[1] = 'mon';
        $haftanin_isimleri[2] = 'tue';
        $haftanin_isimleri[3] = 'wed';
        $haftanin_isimleri[4] = 'thu';
        $haftanin_isimleri[5] = 'fri';
        $haftanin_isimleri[6] = 'sat';
        $haftanin_isimleri[7] = 'sun';

        foreach($haftanin_gunu AS $h_gunu){
            if($h_gunu >$bugunun_gunu){
                $h_gunu_ver = $h_gunu;
                break;
            }
        }

        // if rules
        // If today is not among the specified day or days of the week, give the day of the next week.
        // If the day or days of the specified week are equal to today and the specified time is less than today's time, give the day of the next week.
        // If the specified day or days of the week are equal to today, the specified hour is equal to today's time, the specified minute is less than or equal to today's minute, give the day of the next week.
        if (!in_array($bugunun_gunu, $haftanin_gunu) || (in_array($bugunun_gunu, $haftanin_gunu) && ($saat > -1 && $saat < $bugunun_saati || ($saat > -1 && $saat == $bugunun_saati && $dakika > -1 && $dakika <= $bugunun_dakikasi)))) {
            //$tarih->modify(date('Y-m-d', strtotime("next ".$haftanin_isimleri[$h_gunu_ver])));
            $tarih->modify("next ".$haftanin_isimleri[$h_gunu_ver]);
            $tarih->setTime($bugun->format('H'), $bugun->format('i'));
        }
}
//} // if (strpos($saat, '/') === false) {
##############################################################################################################################################################

// Saat kontrolü
if ($saat == -1) {
    // If the time is * (asterisk) selected, give +1 hour forward value.

    // If minute value is selected
    // If the selected minute is less than the current minute, it means the minute has passed.
    // If the selected day and the day in the transaction are equal
    if ($dakika > -1 && $bugun->format('i') > $dakika && $tarih->format('d') == $bugun->format('d')) {
        $tarih->modify('+1 hour'); // +1 saat ayarla
    }else{
       $tarih->modify('+1 hour'); // +1 saat ayarla
    }
} else {
    // If the hour value refers to a specific time interval
    if (strpos($saat, '/') !== false) {

        list($eksibir, $ozelzaman) = explode('/', $saat);

        // Does your custom hour value also include 0.30 for every half hour?
        // And if the current minute is less than 30
        if (strpos($ozelzaman, '.') !== false && 30 > $bugun->format('i')) {
            // Set current hour and minute to 30
            $tarih->setTime($bugun->format('H'), 30);

        }else if(strpos($ozelzaman, '.') !== false && 30 < $bugun->format('i')){

            // If the current minute value of 0.30 in the special hour value is greater than 30, move it forward one hour and set the minute to 30.
            $tarih->modify('+1 hour'); // Set the clock forward 1 hour
            $tarih->setTime($tarih->format('H'), 30); // Fix minutes at 30

        }else if($ozelzaman == 12){
            // If the custom hour value is 12, set it to 12 noon and 00 past.
            if($bugun->format('H') > 12){
                // If the current time is greater than 12, set it to midnight 00:00
                $tarih->setTime(0, 0);
            }else{
                // If the current time is less than 12, set it to 12:00 noon
                $tarih->setTime(12, 0);
            }
        }else{
            // For the custom hour value, advance the clock by the specified hour value for the normal value of every x hours.
            $tarih->modify(+$ozelzaman. 'hour');
            $tarih->setTime($tarih->format('H'), 0); // Fix minutes at 0
        }


    } else { // if (strpos($saat, '/') !== false) {
        // If a normal, non-special time is selected

        // If the current time is less than the specified time, it means it is not yet time.
        // If the current month and the set month are the same
        if($bugun->format('H') < $saat && $tarih->format('m') == $bugun->format('m')){
            // If the current time is less than the specified time, set the same time
            $tarih->setTime($saat, 0);
        }else{
            $tarih->setTime($saat, $bugun->format('i'));
        }
    }
}
##############################################################################################################################################################




##############################################################################################################################################################
// Minute control
if (strpos($saat, '/') === false) {

// If minute * (asterisk) is selected and Hour 0 is not selected
if ($dakika == -1 && $saat >0) {
    // If minute * (asterisk) is selected, give +1 minute forward value.
    $tarih->modify('+1 minute');
} else {
    // If a specific minute is selected, set that minute
    // If the hour is 0, let's set the minutes to 0 so that it is 00:00.
    if($saat ==0){
        $tarih->setTime($tarih->format('H'), 0);
    }else{
        $tarih->setTime($tarih->format('H'), $tarih->format('i'));
        $tarih->modify('+1 minute');
    }
    
}
}
##############################################################################################################################################################




##############################################################################################################################################################




// Get Unix timestamp
$unix_zaman = $tarih->format('U');

// View result
echo "<br>";
echo date('Y-m-d H:i', $unix_zaman);
echo "<br>";
echo date('j F Y l, H:i', $unix_zaman);
echo "<br>";
########################################################################################################

}

?>
<meta charset="UTF-8">
<form method="POST" action="">

<table border="1">

</tbody>

    <tr>
        <td colspan="2">When the day of the week is selected, the day of the month is ignored<br /><br /><br />Selecting (*) star disables the day of the week, enables the day of the month</td>
        <td colspan="3" style="padding: 0rem 0.75rem 0rem 0.75rem;vertical-align: middle;">
        <select size="8" name="haftanin_gunu[]" id="haftanin_gunu" style="width: 150px;" multiple>
            <?php
                $haftanin_gunleri = array(-1 => '*', 1 => "Pazartesi","Salı","Çarşamba","Perşembe","Cuma","Cumartesi","Pazar");
                $post_gunler = isset($_POST['haftanin_gunu']) ? $_POST['haftanin_gunu'] : [];
                foreach($haftanin_gunleri as $ky => $haftanin_gunu)
                {
                    if(in_array($ky, $post_gunler)){
                        echo "<option value=\"$ky\" selected>$haftanin_gunu</option>\n";
                    }else{
                        if(count($post_gunler)==0 && $ky == -1){
                            echo "<option value=\"$ky\" selected>$haftanin_gunu</option>\n";
                        }else{
                            echo "<option value=\"$ky\">$haftanin_gunu</option>\n";
                        }
                        
                    }
                }
            ?>
        </select>
        </td>
        <td colspan="3"></td>
    </tr>

    <tr>
        <td>Day / Hour / Minute</td>
        <td style="text-align:right;">Day:</td>
        <td style="padding: 0rem 0.75rem 0rem 0.75rem;vertical-align: middle;">
            <select name="gun" id="gun">
              <option value="-1" selected="selected">*</option>       
              <?php
              for ($i=01;$i<=31;$i++){
                if(isset($_POST['gun']) && $_POST['gun'] == $i){
                    echo "<option value=\"$i\" selected>$i</option>\n";
                }else{
                    echo "<option value=\"$i\">$i</option>\n";
                }
              }     
              ?>
            </select>
        </td>
        <td style="text-align:right;">Hour:</td>
        <td style="padding: 0rem 0.75rem 0rem 0.75rem;vertical-align: middle;">
            <select name="saat" id="saat">
                <?php
                $options = array(
                    array("-1", "Every Hour(*)"),
                    array("-1/0.30", "Two an Hour(*/0.30)"),
                    array("-1/2", "Every Two Hours(*/2)"),
                    array("-1/3", "Every Three Hours(*/3)"),
                    array("-1/4", "Every Four Hours(*/4)"),
                    array("-1/6", "Every Six Hours(*/6)"),
                    array("-1/12", "Every Twelve Hours(0,12)"),
                    array("0", "Midnight(00:00)")
                );

                foreach ($options as $option) {
                    $value = $option[0];
                    $label = $option[1];
                    $selected = (isset($_POST['saat']) && $_POST['saat'] == $value) ? "selected" : "";
                    echo "<option value=\"$value\" $selected>$label</option>\n";
                }

                for ($i = 1; $i <= 23; $i++) {
                    $selected = (isset($_POST['saat']) && $_POST['saat'] == $i) ? "selected" : "";
                    echo "<option value=\"$i\" $selected>$i</option>\n";
                }
                ?>
            </select>        
        </td>
        <td>Minute:</td>
        <td style="padding: 0rem 0.75rem 0rem 0.75rem;vertical-align: middle;">
            <select name="dakika" id="dakika">
              <option value="-1" selected="selected">*</option>       
              <?php
              for ($i=0;$i<=59;$i++){
                if(isset($_POST['dakika']) && $_POST['dakika'] == $i){
                    echo "<option value=\"$i\" selected>$i</option>\n";
                }else{
                    echo "<option value=\"$i\">$i</option>\n";
                }
              }    
              ?>
            </select>
        </td>
    </tr>

    <tr>
        <td colspan="8" align="center"><button type="submit"> Create Date </button></td>
    </tr>
</table>
</form>
Sponsor our Newsletter | Privacy Policy | Terms of Service