Count days from registration date to today

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.

Leave a Comment