I want to set scheduler through cron in php

by Amol   Last Updated October 14, 2017 14:05 PM

  1. My CRON job is running every 5 min on server.

  2. I want to run a function through a CRON when it will satisfy below criteria: i. I want to set a scheduler with following options which user will select: Start Date & Time: user will select start date & time for scheduler Scheduler ruccurance : daily/weekly/monthly (User can select from dropdrown) Scheduler to run number of times: 1 to 24 (User can select from dropdrown) (Ex. 1: function will run 6 times daily, Ex. 2: function will run 2 times weekly, etc...)\

  3. I'm storing all the above thing in DB in the below table format (You can correct me there if anything wrong, or with the new table format):

I did following things: Please help to complete the things, My code is not working properly or I may doing some long or wrong things, please correct my code:

public function run_sm_cron() {

        $scheduler_data = $this->admin_model->get_scheduler();  // Get all records of scheduler

        foreach ($scheduler_data as $scheduler) {   // I have 5-6 records in db, it will go through each records
            if ($run_at = $this->is_active_for_cron($scheduler)) {  // Here, for this logic i need help
                // This function ( is_active_for_cron() )check for the scheduler criteria, if satisfied then will enter here.
                // If rec_period is daily and rec_period is 12, 
                //then control should come in this function 12 times daily from the cron which is running after each 5 min


                // here i'm doing my other imp things to set
            }
        }

    }


    public function is_active_for_cron($scheduler) {
        // get_db_date() : this is custom function, which returns date in expected format

        $current_time = get_db_date();  
        $start_date = get_db_date($scheduler['start_date'] . $scheduler['start_time']);
        if ($start_date <= $current_time) {
            $recurrence_pattern = $scheduler['recurrence_pattern'];
            $recurrence_period = $scheduler['recurrence_period'];
            $run_status = $scheduler['run_status']; // Check how many times ran already 

            switch ($recurrence_pattern) {
            case 'Daily': {
                        $total_time = 24 * 60;  //Convert in minutes
                        $recurrence_time = floor(24 / $recurrence_period) * 60;

                        $add_time = $recurrence_time * $run_status * 60;

                        $add_next_time = $recurrence_time * ($run_status + 1) * 60; // i'm checking for next time to run as per schedule
                        $run_at = date("H:i", strtotime($start_date) + $add_time);
                        $run_at = get_db_date(get_db_date('', DATE_YMD) . $run_at); // Get time and date to run next time

                        $run_next_at = date("Y-m-d H:i:s", strtotime("+" . ($recurrence_time / 60) . " hours", strtotime($current_time)));

                    if ($recurrence_period == $run_status) {   // stop execution in before last scheduler,...adjustment
                        $this->admin_model->cron_run_completed($scheduler['api_id'], 0);    
                        // Run scheduler n number of times, after completed n numbers make it 0....To know how many times to run or to stop
                    }

                    if (strtotime($current_time) > strtotime($run_at) && strtotime($current_time) < strtotime($run_next_at)) {
                        // If current time is grater than run at   and current time is less than next running time

                        $this->admin_model->cron_run_completed($scheduler['api_id']);   // increment run_times in db...To know how many times it ran already 
                        return $run_at; // return true if control comes here
                    }




                        break;
            }

            case 'Weekly': {


                break;
            }

            case 'Monthly': {


                break;
            }

        }

    }
Tags : php mysql cron


Related Questions


Handle cron jobs for multiple timezone

Updated December 12, 2016 08:02 AM

Executing a script continuously

Updated April 22, 2015 21:02 PM

Send signal to a scala process

Updated July 20, 2015 14:02 PM

Failover for High Frequency Jobs

Updated May 13, 2015 12:40 PM

How to write a web based scheduling service

Updated April 20, 2015 09:02 AM