$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugins - WP_CRON issue with UTC and local time|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugins - WP_CRON issue with UTC and local time

matteradmin9PV0评论

I have made a plugin to change some user metadata on a schedule according to the wp_cron function.

If the current time is say between their lunch hours (that is in their user profile as date('g:i a', strtotime($time)

However whenever the Cron Job is run, it is in UTC and the other times in the user profile is in local time.

I'm just hitting my head against a wall because I can't figure it out.

// function to run
function mb_user_location_automater() {

    // get the read values
    $user_id            = get_current_user_id();
    $current_time       = time();

    // defaults
    $default_start      = date( "Y-m-d 09:00", strtotime('today') );
    $default_finish     = date( "Y-m-d 17:00", strtotime('today') );
    $default_lunch_in   = date( "Y-m-d 12:00", strtotime('today') );
    $default_lunch_out  = date( "Y-m-d 13:00", strtotime('today') );
    $is_weekend         = date( 'N', strtotime('tomorrow') );

    // get the user meta
    $user_start         = get_user_meta( $user_id, 'mbu_start_time',    true );
    $user_finish        = get_user_meta( $user_id, 'mbu_finish_time',   true );
    $user_lunch_in      = get_user_meta( $user_id, 'mbu_lunch_start',   true );
    $user_lunch_out     = get_user_meta( $user_id, 'mbu_lunch_finish',  true );

    // set times if not set in user settings
    $user_start         = ( empty($user_start)      ? $default_start        : $user_start       );
    $user_finish        = ( empty($user_finish)     ? $default_finish       : $user_finish      );
    $user_lunch_in      = ( empty($user_lunch_in)   ? $default_lunch_in     : $user_lunch_in    );
    $user_lunch_out     = ( empty($user_lunch_out)  ? $default_lunch_out    : $user_lunch_out   );


    // set user to: away
    if( ($current_time < $user_start) && ($current_time > $user_finish) ) {
        update_user_meta( $user_id, 'mblocation', 'away' );
    }

    // set user to: lunch
    if( ($current_time > $user_lunch_in) && ($current_time < $user_lunch_out) ) {
        update_user_meta( $user_id, 'mblocation', 'lunch' );
    }

    // set user to: weekend
    if( $tomorrow_wknd >= 6 ) {
        update_user_meta( $user_id, 'mblocation', 'weekend' );
    }

}

// generate the intervals
function mb_task_intervals( $schedules ) {
    $schedules[ 'every_60_seconds' ] = array(
        'interval'  => ( 60 * 1 ),
        'display'   => 'Every 60 seconds',
    );  

    return $schedules;
}



// set the task schedule
function mb_task_start() {

    // when are operating hours
    $task_start_time    = strtotime( "Today 6:00am"  );
    $task_stop_time     = strtotime( "Today 10:00pm" );

    // check if in operation time
    if( $task_start_time < time() && time() > $task_stop_time ) {
        if( ! wp_next_scheduled( 'mb_user_location_automater' )) {
            wp_schedule_event( time(), 'every_60_seconds', 'mb_user_location_automater' );
        }
    } else {
        // run the stop function
        if( ! wp_next_scheduled( 'mb_task_stop' )) {
            wp_schedule_single_event( time(), 'mb_task_stop' );
        }
    }
}

I have made a plugin to change some user metadata on a schedule according to the wp_cron function.

If the current time is say between their lunch hours (that is in their user profile as date('g:i a', strtotime($time)

However whenever the Cron Job is run, it is in UTC and the other times in the user profile is in local time.

I'm just hitting my head against a wall because I can't figure it out.

// function to run
function mb_user_location_automater() {

    // get the read values
    $user_id            = get_current_user_id();
    $current_time       = time();

    // defaults
    $default_start      = date( "Y-m-d 09:00", strtotime('today') );
    $default_finish     = date( "Y-m-d 17:00", strtotime('today') );
    $default_lunch_in   = date( "Y-m-d 12:00", strtotime('today') );
    $default_lunch_out  = date( "Y-m-d 13:00", strtotime('today') );
    $is_weekend         = date( 'N', strtotime('tomorrow') );

    // get the user meta
    $user_start         = get_user_meta( $user_id, 'mbu_start_time',    true );
    $user_finish        = get_user_meta( $user_id, 'mbu_finish_time',   true );
    $user_lunch_in      = get_user_meta( $user_id, 'mbu_lunch_start',   true );
    $user_lunch_out     = get_user_meta( $user_id, 'mbu_lunch_finish',  true );

    // set times if not set in user settings
    $user_start         = ( empty($user_start)      ? $default_start        : $user_start       );
    $user_finish        = ( empty($user_finish)     ? $default_finish       : $user_finish      );
    $user_lunch_in      = ( empty($user_lunch_in)   ? $default_lunch_in     : $user_lunch_in    );
    $user_lunch_out     = ( empty($user_lunch_out)  ? $default_lunch_out    : $user_lunch_out   );


    // set user to: away
    if( ($current_time < $user_start) && ($current_time > $user_finish) ) {
        update_user_meta( $user_id, 'mblocation', 'away' );
    }

    // set user to: lunch
    if( ($current_time > $user_lunch_in) && ($current_time < $user_lunch_out) ) {
        update_user_meta( $user_id, 'mblocation', 'lunch' );
    }

    // set user to: weekend
    if( $tomorrow_wknd >= 6 ) {
        update_user_meta( $user_id, 'mblocation', 'weekend' );
    }

}

// generate the intervals
function mb_task_intervals( $schedules ) {
    $schedules[ 'every_60_seconds' ] = array(
        'interval'  => ( 60 * 1 ),
        'display'   => 'Every 60 seconds',
    );  

    return $schedules;
}



// set the task schedule
function mb_task_start() {

    // when are operating hours
    $task_start_time    = strtotime( "Today 6:00am"  );
    $task_stop_time     = strtotime( "Today 10:00pm" );

    // check if in operation time
    if( $task_start_time < time() && time() > $task_stop_time ) {
        if( ! wp_next_scheduled( 'mb_user_location_automater' )) {
            wp_schedule_event( time(), 'every_60_seconds', 'mb_user_location_automater' );
        }
    } else {
        // run the stop function
        if( ! wp_next_scheduled( 'mb_task_stop' )) {
            wp_schedule_single_event( time(), 'mb_task_stop' );
        }
    }
}
Share Improve this question asked Nov 29, 2018 at 0:25 markbmarkb 2996 silver badges18 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

It seems you are comparing GMT time with the local time before you update the user meta.

Try WP function current_time( $type, $gmt = 0 );

$current_time = current_time('timestamp', 0) // local time
$current_time = current_time('timestamp', 1) // GMT time

I think you need

$current_time = current_time('timestamp', 0) // local time

instead of

$current_time       = time();

Also take a look at current_time function

Edit:

Also i noticed your code where:

if( ($current_time < $user_start) && ($current_time > $user_finish) ) {
        update_user_meta( $user_id, 'mblocation', 'away' );
    }

$current_time is in Unix timestamp format and the $user_start or $user_finish is in formatted date, you should convert $user_start and $user_finish to timestamp as well. You need to do the same with other code where it is applicable.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far