最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - delete an array element when its date expires

programmeradmin2浏览0评论

I have user levels for access content. I made a start date and end date input for each other. Each level has its own history. Whichever level date exceeds today's date, it will be removed from user levels array, but all of them are removed in my function. How can I do that?

This function is just adds levels with end dates.

add_action( 'show_user_profile', 'crf_show_extra_profile_fields' );
add_action( 'edit_user_profile', 'crf_show_extra_profile_fields' );

function crf_show_extra_profile_fields( $user ) {
  $choices = [
    '+15 minutes'  => '2 Hafta',
    '+9 months' => '9 Ay',
    '+12 months' => '12 Ay',
    '+2 minutes' => 'Uzat',
];
$rua_user = rua_get_user( $user->ID );
$user_levels = $rua_user->get_level_ids();
$current_date = date("d/m/y H:i:s");

  foreach ( $user_levels as $user_level ) {

    $start_time_meta = 'start-time-'.$user_level;
    $start_time = get_the_author_meta( $start_time_meta, $user->ID);

    $end_time_meta = 'end-time-'.$user_level;
    $end_time = get_the_author_meta( $end_time_meta, $user->ID);

    $effectiveDate = strtotime($end_time, strtotime($start_time));
    $time = date("d/m/y H:i:s", $effectiveDate);

    ob_start();

    echo esc_html( get_the_title( $user_level ) ) . ' -> ';
    ?>
    <input type="datetime-local" id="start-time-<?php echo esc_attr( $user_level ); ?>" name="start-time-<?php echo esc_attr( $user_level ); ?>" value="<?php echo esc_attr( $start_time ); ?>"> - 

    <select name='end-time-<?php echo esc_attr( $user_level ); ?>' id='end-time-<?php echo esc_attr( $user_level ); ?>' style='width:180px;'>
        <option value='default'>Süre Seçiniz</option>
        <?php
        foreach ( $choices as $value => $label ) {
            ?>
            <option value="<?php echo esc_attr( $value ); ?>"
                <?php selected( $value, $end_time ); ?>>
                <?php echo esc_html( $label ); ?>
            </option>
            <?php
        }
        ?>
    </select> <?php echo $time; ?>
    <br>
    <br>
    <?php
    $html = ob_get_clean();
    $elements[] = $html;
  } ?>

    <table class="form-table">
        <tr>
            <th><?php esc_html_e( 'Kitaplar', 'crf' ); ?></th>
            <td>
                <?php
                    sort($elements);
                    echo (vsprintf(__('%s'),implode("",$elements )));
                ?>
            </td>
        </tr>
    </table>


    <?php
}

This function just delete the levels but I want to delete if a level's date is expires:

    function my_demo_cronjob_action () {
        $present_date = date("Y-m-d H:i:s");
        $user = get_user_by('id',26);
        $user_levels = rua_get_user($user)->get_level_ids(false, false, true);
    
        foreach ($user_levels as $level) {
    
            $end_time_meta = 'end-time-'.$level;
            $end_time = get_the_author_meta( $end_time_meta, $user);
    
            $effectiveDate = strtotime($end_time, strtotime($present_date));
            $time = date("d/m/y H:i:s", $effectiveDate);
    
            if ($present_date > $time) {
                rua_get_user($user)->remove_level($level);
            }
        }
    }
    add_action('my_demo_cronjob_action', 'my_demo_cronjob_action');

the cronjob function is executed by a cron plugin

发布评论

评论列表(0)

  1. 暂无评论