我制作了一个插件,根据wp\\u cron函数按计划更改一些用户元数据。
如果当前时间介于他们的午餐时间之间(在他们的用户配置文件中为date(\'g:i a\', strtotime($time)
但是,每当运行Cron作业时,它都是UTC时间,用户配置文件中的其他时间都是本地时间。
我只是把头撞在墙上,因为我想不出来。
// 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\' );
}
}
}
最合适的回答,由SO网友:Awais 整理而成
在更新用户元之前,您似乎正在将GMT时间与本地时间进行比较。
尝试WP功能current_time( $type, $gmt = 0 );
$current_time = current_time(\'timestamp\', 0) // local time
$current_time = current_time(\'timestamp\', 1) // GMT time
我想你需要
$current_time = current_time(\'timestamp\', 0) // local time
而不是
$current_time = time();
还可以看看
current_time 作用
编辑:
我还注意到您的代码:
if( ($current_time < $user_start) && ($current_time > $user_finish) ) {
update_user_meta( $user_id, \'mblocation\', \'away\' );
}
$current_time
是Unix时间戳格式
$user_start
或
$user_finish
是格式化日期,您应该转换
$user_start
和
$user_finish
也要加时间戳。在适用的地方,您需要对其他代码执行相同的操作。