原因是当您只设置了一个没有日期的时间时,php函数strtotime()
默认情况下,将今天添加为日期,因此如果时间为;04:30“;,时间戳在过去。
我这样修复:
$timestamp = strtotime( $cron_sync_time );
if( $timestamp < time() ){ //if the time already past today
$timestamp = $timestamp + 60 * 60 * 24; //add 1 day
}
wp_schedule_event( $timestamp, \'daily\', $hook);
Full 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
$timestamp = strtotime( $cron_sync_time );
if( $timestamp < time() ){
$timestamp = $timestamp + 60 * 60 * 24;
}
wp_schedule_event( $timestamp, \'daily\', $hook);
update_option( "cron_time_used", $cron_sync_time );
}
}