Skip to main content
deleted 5 characters in body
Source Link
J.BizMai
  • 900
  • 2
  • 10
  • 30

I´m doing some setting to enable/disable a cron task and choose a time to run this task.

When I disable the cron task and enable it after without changed the time, my timestamp is in the past and make this error :

WP Control Plugin Result

enter image description here

My code

function scheduled_task_activation(){
    $hook = 'my_hook';
    $options_values = get_option( "option_name" );
    $is_cron_active = (!empty( $options_values['cron-sync-active'] ) ) ? true : false;
    $cron_sync_time = (!empty( $options_values['cron-sync-time'] ) ) ? $options_values['cron-sync-time']: "00:00:00";
    if( !$is_cron_active ){
        if( wp_next_scheduled( $hook ) ){
            wp_clear_scheduled_hook( $hook );
        }
    }
    else if ( ! wp_next_scheduled( $hook ) || ( $cron_sync_time !== get_option( APIMO_CRON_TIME_USED "cron_time_used") )  ) {
        if( $cron_sync_time !== get_option( "cron_time_used" ) )
            wp_clear_scheduled_hook( $hook ); //avoid dupplication
        var_dump( $cron_sync_time ); //Output : (string) "04:30"
        wp_schedule_event( strtotime($cron_sync_time), 'daily', $hook);
        update_option( "cron_time_used", $cron_sync_time );
    }
}

If $cron_sync_time is a string "04:30", why the timestamp should be in the past ? Someone knows the way to fix this ?

I´m doing some setting to enable/disable a cron task and choose a time to run this task.

When I disable the cron task and enable it after without changed the time, my timestamp is in the past and make this error :

WP Control Plugin Result

enter image description here

My code

function scheduled_task_activation(){
    $hook = 'my_hook';
    $options_values = get_option( "option_name" );
    $is_cron_active = (!empty( $options_values['cron-sync-active'] ) ) ? true : false;
    $cron_sync_time = (!empty( $options_values['cron-sync-time'] ) ) ? $options_values['cron-sync-time']: "00:00:00";
    if( !$is_cron_active ){
        if( wp_next_scheduled( $hook ) ){
            wp_clear_scheduled_hook( $hook );
        }
    }
    else if ( ! wp_next_scheduled( $hook ) || ( $cron_sync_time !== get_option( APIMO_CRON_TIME_USED ) )  ) {
        if( $cron_sync_time !== get_option( "cron_time_used" ) )
            wp_clear_scheduled_hook( $hook ); //avoid dupplication
        var_dump( $cron_sync_time ); //Output : (string) "04:30"
        wp_schedule_event( strtotime($cron_sync_time), 'daily', $hook);
        update_option( "cron_time_used", $cron_sync_time );
    }
}

If $cron_sync_time is a string "04:30", why the timestamp should be in the past ? Someone knows the way to fix this ?

I´m doing some setting to enable/disable a cron task and choose a time to run this task.

When I disable the cron task and enable it after without changed the time, my timestamp is in the past and make this error :

WP Control Plugin Result

enter image description here

My code

function scheduled_task_activation(){
    $hook = 'my_hook';
    $options_values = get_option( "option_name" );
    $is_cron_active = (!empty( $options_values['cron-sync-active'] ) ) ? true : false;
    $cron_sync_time = (!empty( $options_values['cron-sync-time'] ) ) ? $options_values['cron-sync-time']: "00:00:00";
    if( !$is_cron_active ){
        if( wp_next_scheduled( $hook ) ){
            wp_clear_scheduled_hook( $hook );
        }
    }
    else if ( ! wp_next_scheduled( $hook ) || ( $cron_sync_time !== get_option( "cron_time_used") )  ) {
        if( $cron_sync_time !== get_option( "cron_time_used" ) )
            wp_clear_scheduled_hook( $hook ); //avoid dupplication
        var_dump( $cron_sync_time ); //Output : (string) "04:30"
        wp_schedule_event( strtotime($cron_sync_time), 'daily', $hook);
        update_option( "cron_time_used", $cron_sync_time );
    }
}

If $cron_sync_time is a string "04:30", why the timestamp should be in the past ? Someone knows the way to fix this ?

Source Link
J.BizMai
  • 900
  • 2
  • 10
  • 30

Cron task with scheduled timestamp in the past

I´m doing some setting to enable/disable a cron task and choose a time to run this task.

When I disable the cron task and enable it after without changed the time, my timestamp is in the past and make this error :

WP Control Plugin Result

enter image description here

My code

function scheduled_task_activation(){
    $hook = 'my_hook';
    $options_values = get_option( "option_name" );
    $is_cron_active = (!empty( $options_values['cron-sync-active'] ) ) ? true : false;
    $cron_sync_time = (!empty( $options_values['cron-sync-time'] ) ) ? $options_values['cron-sync-time']: "00:00:00";
    if( !$is_cron_active ){
        if( wp_next_scheduled( $hook ) ){
            wp_clear_scheduled_hook( $hook );
        }
    }
    else if ( ! wp_next_scheduled( $hook ) || ( $cron_sync_time !== get_option( APIMO_CRON_TIME_USED ) )  ) {
        if( $cron_sync_time !== get_option( "cron_time_used" ) )
            wp_clear_scheduled_hook( $hook ); //avoid dupplication
        var_dump( $cron_sync_time ); //Output : (string) "04:30"
        wp_schedule_event( strtotime($cron_sync_time), 'daily', $hook);
        update_option( "cron_time_used", $cron_sync_time );
    }
}

If $cron_sync_time is a string "04:30", why the timestamp should be in the past ? Someone knows the way to fix this ?