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

Count days from registration date to today

programmeradmin3浏览0评论

I'd like to add extra text to users who signed up for more the x days.

My function is as follows:

   $today           = current_time( 'mysql' );
   $days_diff       = date( 'd', $today);   
   $register_date   = strtotime( get_the_author_meta( 'user_registered', $post_author_id ) );
   $register_on = date_i18n( get_option('date_format'), $register_date );

   echo sprintf( __( '%s ago', 'kiwi' ), apply_filters( 'string_format_i18n', human_time_diff( $register_date ) ) );

When I use a var_dump on $today, $days_diff, and $register_date

string(19) "2016-06-13 13:50:38" 
string(2) "01" 
string(12) "June 3, 2016"

The problem is $days_diff. Anyone an idea how to change this variable so that it will counts the days from registration date to $today?

I'd like to add extra text to users who signed up for more the x days.

My function is as follows:

   $today           = current_time( 'mysql' );
   $days_diff       = date( 'd', $today);   
   $register_date   = strtotime( get_the_author_meta( 'user_registered', $post_author_id ) );
   $register_on = date_i18n( get_option('date_format'), $register_date );

   echo sprintf( __( '%s ago', 'kiwi' ), apply_filters( 'string_format_i18n', human_time_diff( $register_date ) ) );

When I use a var_dump on $today, $days_diff, and $register_date

string(19) "2016-06-13 13:50:38" 
string(2) "01" 
string(12) "June 3, 2016"

The problem is $days_diff. Anyone an idea how to change this variable so that it will counts the days from registration date to $today?

Share Improve this question asked Jun 13, 2016 at 13:54 kiarashikiarashi 2944 silver badges16 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I am using this answer from StackOverflow as a basis which using functions from PHP 5.2.2


The main issue is that human_time_diff() doesn't always return days, nor does it have a parameter on what to return. This function just returns how long they have been registered, whether it's days, weeks, months, or years. I believe the most reliable solution is to do this all in PHP really:

$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

if( $interval_obj->days > 1 ) {             // The most commonly hit condition at the top
    echo __( "Registered {$interval_obj->days} days ago", "kiwi" );
} elseif( 0 == $interval_obj->days ) {      // IF they registered today
    echo __( 'Registered Today', 'kiwi' );
} elseif( 1 == $interval_obj->days ) {      // IF they registered yesterday
    echo __( 'Registered Yesterday', 'kiwi' );
} else {                                    // The off-chance we have less than zero
    echo __( 'Registered', 'kiwi' );
}

In this answer we are using the PHP DateTime Class to get what we need. I removed the i18n functions and filters and I don't believe they're needed since we are just dealing with integers and not a specific date / time.

If the code-comments need more clarification, leave a comment below and I'll expand on it.

发布评论

评论列表(0)

  1. 暂无评论