Change user registration date output from unix to mm/dd/yyyy

I have this code for our member information page.
You joined WFAH on:
[s2Get constant=“S2MEMBER_CURRENT_USER_REGISTRATION_TIME” /]

It outputs the date in UNIX. I need it to output the date formatted as mm/dd/yyyy. I’ve tried a couple of ways but am messing up somewhere and we’ve had a couple of instances where paypal has refunded users cause they said we don’t tell them, even though the reminder email goes out.

There has to be a way to do this, surely.

add the following to your functions.php

function custom_registration_date_shortcode() {
	if (is_user_logged_in()) {
		$user = wp_get_current_user();
		$registered = strtotime($user->user_registered);
		return date('m/d/Y', $registered);
	}
	return '';
}
add_shortcode('registration_date', 'custom_registration_date_shortcode');

then use the shortcode [registration_date]

1 Like

Thanks so much for quick response.
additional questions:
do I have to create a child theme to do this?
or, can I copy and paste to the bottom of the php.functions file in the main Corp theme?

You don’t have to but I don’t enjoy replacing the functions.php file every time the theme is upgraded, so I would If I were you.

I would respectfully suggest to @clavaque to add this to the s2get shortcode

in the same way as s2member_auto_eot_time and s2member_last_payment_time, so that the date/time a user joined can easily be displayed. It’d be super useful and save people needing to code up their own version.

Cheers! :beers:

[s2Get user_field="s2member_auto_eot_time" date_format="M jS, Y, g:i a T" /] # Auto EOT-Time for the current User (when applicable). Use date_format="" with any PHP date formatting chars you like. If you don't pass this, a timestamp is given instead. See: http://php.net/manual/en/function.date.php
[s2Get user_field="s2member_last_payment_time" date_format="M jS, Y, g:i a T" /] # Timestamp. Last time an actual payment was received by s2Member. Use date_format="" with any PHP date formatting chars you like. If you don't pass this, a timestamp is given instead. See: http://php.net/manual/en/function.date.php
1 Like

for what its worth the addition can be implemented with adding…

on line 83 of s2member/src/includes/classes/sc-gets-in.inc.php

from

			$get = constant($attr['constant']);

to

			$get = constant($attr['constant']);

			if (preg_match('/time$/i', strtolower($attr['constant'])) && $attr['date_format'])
				if ((is_numeric($get) && strlen($get) === 10) || ($get = strtotime($get))) // Timestamp?
				{
					if ($attr['date_format'] === 'timestamp')
						$get = (string)$get; // No change.

					else if ($attr['date_format'] === 'default')
						$get = date(get_option('date_format'), (int)$get);

					else $get = date($attr['date_format'], (int)$get);
				}

which adds the following to s2Get

[s2Get constant=“S2MEMBER_CURRENT_USER_REGISTRATION_TIME” date_format=“M jS, Y, g:i a T” /]

until then replace the previous function with this one as it will allow you to defined the date format

function custom_registration_date_shortcode( $atts ) {
    if ( ! is_user_logged_in() ) {
        return '';
    }

    // Define default attributes
    $atts = shortcode_atts(
        array(
            'date_format' => 'm/d/Y', // Default format
        ),
        $atts,
        'registration_date'
    );

    $user       = wp_get_current_user();
    $registered = strtotime( $user->user_registered );

    // Use the provided format (sanitized via shortcode_atts)
    return date( $atts['date_format'], $registered );
}

Nice idea. :slight_smile: