doudi7782 2019-07-16 19:57
浏览 83

计算注册日期和1年之间的天数

I am trying to calculate the difference and output it like this: "Your membership will be renewed in XXX days."

This is the code I am using ->

add_shortcode( 'membership', 'pren_info');
function pren_info() {
    $pren = wp_get_current_user();
    $first_name = $pren->first_name;
    $last_name = $pren->last_name;
    $date_format = 'j M Y H:i';

    $today_obj      = new DateTime( date( 'Y-m-d', strtotime( 'today' ) ) );            // Get today's Date Object
    $register_date  = get_the_author_meta( 'user_registered', get_current_user_id() );  // Grab the registration Date
    $registered_obj = new DateTime( date( 'Y-m-d', strtotime( $register_date ) ) );     // Get the registration Date Object
    $interval_obj   = $today_obj->diff( $registered_obj );                             // Retrieve the difference Object
    $renewal_date = date('Y-m-d',strtotime('+1 year',$register_date));
    $datediff = floor(strtotime($renewal_date)/(60*60*24)) - floor(strtotime($register_date)/(60*60*24));

    echo '<div class="pren-hello"><b>Hi '.$first_name.'!</b></div>';

    if( $interval_obj->days > 1 ) {             // The most commonly hit condition at the top
    echo __( "Your account was registered {$interval_obj->days} days ago as a member.", "theme" );

    } elseif( 0 == $interval_obj->days ) {      // IF they registered today
    echo __( 'Your account was registered today.', 'theme' );

    } elseif( 1 == $interval_obj->days ) {      // IF they registered yesterday
    echo __( 'Your account was registered yesterday.', 'theme' );

    } else {                                    // The off-chance we have less than zero
    echo __( 'Please come back tomorrow for an update.', 'theme' );
    }

    echo '<div class="pren-info">In '.$datediff.' days, your membership needs to be renewed.</div>';
}

The shortcode works in terms of showing the name and showing how many days ago the user got registered, but what I cannot get working is this:

I need to calculate the time of "1 year from the date the user registered and the difference in days between that date and the date they registered".

This is "hard" to explain :) Does it make sense?

  • 写回答

2条回答 默认 最新

  • donglie7268 2019-07-16 20:06
    关注

    I'm not sure that I got your question right. You need to calculate interval to a future date, is that you case? If so you can use DateInterval:

    $date = new DateTime();
    $date->add(new DateInterval('P1Y'));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)