Cron function set to run once per month not completing 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', 'me@example.com');

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?
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 &amp; 2015-04-15 your 01 is being collapsed to 1, you need to change it to '01'