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
?
1 Answer
Reset to default 1I 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.