0

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' );
        }
    }
}

1 Answer 1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.