0

I have a cron function that is being triggered but not completing. The function is supposed to run only once on the first of every month. The function runs but, I think, gets as far as comparing the $summary_sent variable and then stops. Cron is running once every 3 hours.

By changing the $day_of_month_to_run to the current day and resetting the $summary_sent variable I can successfully run the full function using cron but, when left to run automatically on the first of the month it fails.

variable_set('summary_email_recipients', '[email protected]');

function appointments_cron() {
    send_monthly_summary();
}

function send_monthly_summary() {
    $recipients = variable_get(summary_email_recipients);
    // run on first of the month
    $day_of_month_to_run = 01;
    $date_string = date('Y-m-'.$day_of_month_to_run);
    // only run on the correct day of the month
    if ( date('Y-m-d') == $date_string ) {
        // check if the process was last run today
        // prevents process being run multiple times on the same day
        $summary_sent = variable_get( 'appointment_summary_sent' );
        if ($summary_sent != $date_string) {
            // do stuff
            // set a record of this process
            variable_set( 'appointment_summary_sent', $date_string );
        }
    }
}

Why isn't this running once on the 1st of every month as expected?

1 Answer 1

1

I always struggled to get cron jobs running at the right time until I found Elysia Cron. Inside the configuration screen of Elysia Cron you can specify which cron hooks run when, so the standard hooks can be set to run each hour, others each day, each week, each month, etc. and you don't need to worry about coding the date as you've done.

EDIT: I just ran the following code:

$day_of_month_to_run = 01;
$date_string = date('Y-m-'.$day_of_month_to_run);
dpm($date_string);
dpm(date('Y-m-d'));

The results were 2015-04-1 & 2015-04-15 your 01 is being collapsed to 1, you need to change it to '01'

3
  • Thanks Colin, good suggestion which I may end up having to use but hopefully I can learn why the code isn't working.
    – user11381
    Apr 15, 2015 at 11:19
  • OK see my updated answer Apr 15, 2015 at 11:27
  • Thanks Colin, I guess I never tested on a date with a leading zero. I won't know for sure if this works until the first of May but I'm betting it will so marked as answered. Thanks again.
    – user11381
    Apr 15, 2015 at 12:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.