Operates at a time earlier than the time specified in wp-cron

Asked 2 years ago, Updated 2 years ago, 81 views

The wp-cron specification states that if the site is accessed after the specified date and time,
I thought that if the batch is not running, the batch will be running.
Operates before the specified time.

I thought that the designation of the schedule might be bad, but
I can't find any other way to write.
If anyone knows, please let me know.

The schedule I would like to register is every Wednesday at 13:0:0 and every month at 12:0:0:0.

/**
 * set function
 */
add_action('MD_BlogCronHook', 'MD_BlogDo');
date_default_timezone_set('Asia/Tokyo');

/**
 * wp-cron start
 */
functionMD_BlogCronStart(){
    wp_schedule_event(time(), 'weekly', 'MD_BlogCronHook');
    wp_schedule_event(time(), 'monthly', 'MD_BlogCronHook');
}
register_activation_hook(__FILE__, 'MD_BlogCronStart');
/**
 * wp-cron stop
 */
functionMD_BlogCronStop(){
    wp_clear_scheduled_hook('MD_BlogCronHook');
}
register_deactivation_hook(__FILE__, 'MD_BlogCronStop');


// Add Event Time
add_filter('cron_schedules', 'my_interval');
function my_interval($schedules){
    date_default_timezone_set('Asia/Tokyo');
    $dt_2 = new DateTime ('next Wednesday 130000');
    $difftime=timeCalculation($dt_2);
    $schedules['weekly'] = array('interval'=>$difftime,'display'=>'weekly');
    $dt_2 = new DateTime('first day of next month000000');
    $difftime=timeCalculation($dt_2);
    $schedules['monthly'] = array('interval'=>$difftime,'display'=>'monthly');
    return $schedules;
}

function timeCalculation($addTime){
    $dt = new DateTime('now');
    $d = $addTime->diff($dt, true);
    $dt_array=get_object_vars($d);
    $day=$dt_array["d"]*24*60*60;
    $hour=$dt_array["h"]*60*60;
    $minutes=$dt_array["i"]*60;
    $second = $dt_array["s"];
    return $day + $hour + $minutes + $second;
}

wordpress

2022-09-30 14:57

1 Answers

 function MD_BlogCronStart(){
    $dt = new DateTime('now');
    $today=$dt->format('Y-m-d00:00:00');#Change to time-free
    wp_schedule_event(strtotime($today), 'weekly', 'MD_BlogCronHook');
    wp_schedule_event(strtotime($today), 'monthly', 'MD_BlogCronHook');
}


function timeCalculation($addTime){
    $dt = new DateTime('today'); change from #now to today

I changed it as above.
If I change the second line now to today, I don't need the third line...?

I'll wait with this.


2022-09-30 14:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.