首页 > 解决方案 > 使用间隔(以周数为单位)和开始日期计算未来日期

问题描述

我正在尝试编写一个函数来计算需要检查一件设备的下一个日期。我正在使用下面的代码(它不完整。)

function get_next_check(){

    $today = date(get_option('date_format'));

    $first_check = types_get_field_meta_value( 'first_check', $post_id );

    // Interval is a number of weeks- ie. month = 4, year = 52
    $interval = types_get_field_meta_value( 'interval', $post_id );

    // Calculate the next date after today that the check needs to be performed
    $next_check = ;

    return $next_check;

}
add_shortcode( 'next_check', 'get_next_check' );

我猜我需要创建一个包含所有可能日期的数组,并将每个日期与今天的日期进行比较,只返回下一个日期?

标签: phpwordpress

解决方案


您可以根据 Greg Schmidt 的建议来实现这一点,或者通过执行以下操作:

// Calculate the next date after today that the check needs to be performed
$next_check = date(get_option('date_format'), strtotime($today." ".$interval." weeks"));

推荐阅读